From 1be56221325e8b90883fb0c67a8b86d754c98b37 Mon Sep 17 00:00:00 2001 From: Kieran Millar Date: Mon, 27 Nov 2017 23:41:37 +0000 Subject: [PATCH] More buttons, plus better button drawing code --- gfx/moveToBack_down.bmp | Bin 0 -> 2102 bytes gfx/moveToBack_up.bmp | Bin 0 -> 2102 bytes gfx/moveToFront_down.bmp | Bin 0 -> 2102 bytes gfx/moveToFront_up.bmp | Bin 0 -> 2102 bytes src/Editor/bar.cpp | 145 ++++++++++++++++++++++++++------------- src/Editor/bar.hpp | 27 ++++---- src/Editor/input.cpp | 22 +++++- src/style.cpp | 4 +- 8 files changed, 137 insertions(+), 61 deletions(-) create mode 100644 gfx/moveToBack_down.bmp create mode 100644 gfx/moveToBack_up.bmp create mode 100644 gfx/moveToFront_down.bmp create mode 100644 gfx/moveToFront_up.bmp diff --git a/gfx/moveToBack_down.bmp b/gfx/moveToBack_down.bmp new file mode 100644 index 0000000000000000000000000000000000000000..08be52586162af36c8532a3587f4a04517c35440 GIT binary patch literal 2102 zcmeH`O%8%E5QV4wRN94SaOuVs2?sE2^%C5KLvR&0?%a9<4>P`j7D@u$n3!mI$%K}# zZ#vVo=Sx*kuPP%tVsvn!$N|hB`yohSB0x#ijCoFz{zz>W1`z6#tS0^P z9LGw|m#QGADkC~#bnv0b1I!=$K1g9ALBwTQa$VQlG!5UbCr;^(cp_fR#N4(mcU{M_ zc8jeC+a36yJ7A28$N;AiDd33RK^qag5eoEM92cMF^^Wxk+d=Z}B`X8(nd6z4%jCIp z3Wa}xPw)p^s#?rXT1(whFAdvgV}0P-pL@g;Zb^r}tnvg2q=0a`Z0^@dPL_&dnZudq vISHR=+F$GaSRua$h5jQ0_Aj6nE{;C;;FZDPpZy&tU*{(JfRsGoh>UpyAHgXR literal 0 HcmV?d00001 diff --git a/gfx/moveToFront_down.bmp b/gfx/moveToFront_down.bmp new file mode 100644 index 0000000000000000000000000000000000000000..144b9c2460026ef6e7ce5d7f7ac57ef3f9953a73 GIT binary patch literal 2102 zcmeH`y=}uV5QQJDADSvMgUd9j1bBczZL)+m(GXfCjXSsAAj4eV@hC`^WtIsL=mBSl z`tChaH2MB^^7Z*91um=`>stgO!!XD= zj-tJL?R&7Ff&X&`LU0)c;wB>(T*%;t;eW$nt9mv+@*JK=ToYD)zEuG4QP&9DE;tjGChrcdkros+H4_`PR2`UUQI$DWE;v&dXe!r+RG JG(6`I;Rhc^C)@x4 literal 0 HcmV?d00001 diff --git a/gfx/moveToFront_up.bmp b/gfx/moveToFront_up.bmp new file mode 100644 index 0000000000000000000000000000000000000000..bcf51ef5bba6adbfe830b9995b4189b3b779a35e GIT binary patch literal 2102 zcmeH`F;2ul3`PI!CYhZrI0GdORir%tN(-0BO>&`>G<3AwfWsKuC6eR1=jkDziD*QlGPnH{FMW3i&n9% z&-umMid=u-xUwt0^;f>`zxPZ1D_#2I>f639x6SWc;B9|h<(jnKKWDVZU*{z}KuHd8 Hpi;g7LJcT) literal 0 HcmV?d00001 diff --git a/src/Editor/bar.cpp b/src/Editor/bar.cpp index aa9821b..1d4405b 100644 --- a/src/Editor/bar.cpp +++ b/src/Editor/bar.cpp @@ -54,45 +54,77 @@ void Bar::load( void ) resizeBarScrollRect(window_ptr->width, window_ptr->height); barScrollRect.h = 13; - button_layerBackground_off = NULL; - button_layerBackground_on = NULL; - button_layerTerrain_off = NULL; - button_layerTerrain_on = NULL; - button_layerTool_off = NULL; - button_layerTool_on = NULL; - button_layerVisible_off = NULL; - button_layerVisible_on = NULL; - button_save_down = NULL; - button_save_up = NULL; - - loadButtonGraphic(button_layerBackground_off, "./gfx/layerBackground_off.bmp"); - loadButtonGraphic(button_layerBackground_on, "./gfx/layerBackground_on.bmp"); - loadButtonGraphic(button_layerTerrain_off, "./gfx/layerTerrain_off.bmp"); - loadButtonGraphic(button_layerTerrain_on, "./gfx/layerTerrain_on.bmp"); - loadButtonGraphic(button_layerTool_off, "./gfx/layerTool_off.bmp"); - loadButtonGraphic(button_layerTool_on, "./gfx/layerTool_on.bmp"); - loadButtonGraphic(button_layerVisible_off, "./gfx/layerVisible_off.bmp"); - loadButtonGraphic(button_layerVisible_on, "./gfx/layerVisible_on.bmp"); - loadButtonGraphic(button_save_down, "./gfx/save_down.bmp"); - loadButtonGraphic(button_save_up, "./gfx/save_up.bmp"); - + buttonTexture = SDL_CreateTexture(window_ptr->screen_renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, 512, 32); + SDL_SetTextureBlendMode(buttonTexture, SDL_BLENDMODE_BLEND); + SDL_SetTextureAlphaMod(buttonTexture, 255); + + currentButtonTextureX = 0; + + loadButtonGraphic(button_layerBackground, "./gfx/layerBackground_off.bmp", "./gfx/layerBackground_on.bmp"); + loadButtonGraphic(button_layerTerrain, "./gfx/layerTerrain_off.bmp", "./gfx/layerTerrain_on.bmp"); + loadButtonGraphic(button_layerTool, "./gfx/layerTool_off.bmp", "./gfx/layerTool_on.bmp"); + loadButtonGraphic(button_layerVisible, "./gfx/layerVisible_off.bmp", "./gfx/layerVisible_on.bmp"); + loadButtonGraphic(button_save, "./gfx/save_up.bmp", "./gfx/save_down.bmp"); + loadButtonGraphic(button_moveToBack, "./gfx/moveToBack_up.bmp", "./gfx/moveToBack_down.bmp"); + loadButtonGraphic(button_moveToFront, "./gfx/moveToFront_up.bmp", "./gfx/moveToFront_down.bmp"); } -bool Bar::loadButtonGraphic(SDL_Texture *& texture, const char * filePath) +bool Bar::loadButtonGraphic(buttonInfo & button, const char * filePathUp, const char * filePathDown) { + + SDL_SetRenderDrawColor(window_ptr->screen_renderer, 255, 255, 255, 255); SDL_Surface* graphic = NULL; - SDL_ConvertSurfaceFormat(graphic, SDL_PIXELFORMAT_RGB888, 0); - graphic = SDL_LoadBMP(filePath); + //first load the botton's up graphic + graphic = SDL_LoadBMP(filePathUp); if (graphic == NULL) { - SDL_Log("Unable to load image '%s'! SDL Error: %s\n", filePath, SDL_GetError()); + SDL_Log("Unable to load image '%s'! SDL Error: %s\n", filePathUp, SDL_GetError()); + return false; + } + SDL_ConvertSurfaceFormat(graphic, SDL_PIXELFORMAT_RGB888, 0); + + SDL_Texture * texture = SDL_CreateTextureFromSurface(window_ptr->screen_renderer, graphic); + SDL_Rect rect; + rect.x = currentButtonTextureX; + rect.y = 0; + rect.w = 32; + rect.h = 32; + + SDL_SetRenderTarget(window_ptr->screen_renderer, buttonTexture); + SDL_RenderCopy(window_ptr->screen_renderer, texture, NULL, &rect); + + SDL_FreeSurface(graphic); + graphic = NULL; + SDL_DestroyTexture(texture); + texture = NULL; + button.texOffX = rect.x; + button.texOffY = rect.y; + + currentButtonTextureX += rect.w; + + //then the down graphic + graphic = SDL_LoadBMP(filePathDown); + if (graphic == NULL) + { + SDL_Log("Unable to load image '%s'! SDL Error: %s\n", filePathDown, SDL_GetError()); return false; } texture = SDL_CreateTextureFromSurface(window_ptr->screen_renderer, graphic); + rect.x = currentButtonTextureX; + + SDL_SetRenderTarget(window_ptr->screen_renderer, buttonTexture); + SDL_RenderCopy(window_ptr->screen_renderer, texture, NULL, &rect); SDL_FreeSurface(graphic); graphic = NULL; + SDL_DestroyTexture(texture); + texture = NULL; + + button.texOnX = rect.x; + button.texOnY = rect.y; + + currentButtonTextureX += rect.w; return true; } @@ -251,14 +283,15 @@ void Bar::draw( void ) SDL_RenderDrawLine(window_ptr->screen_renderer, PANEL_WIDTH, canvas_ptr->height, PANEL_WIDTH, window_ptr->height); { + //draw background layer buttons if (type == PERM) - drawButton(button_layerBackground_on, 3, canvas_ptr->height + 3); + drawButton(button_layerBackground, on, 3, canvas_ptr->height + 3); else - drawButton(button_layerBackground_off, 3, canvas_ptr->height + 3); + drawButton(button_layerBackground, off, 3, canvas_ptr->height + 3); if (canvas_ptr->layerVisible[PERM]) - drawButton(button_layerVisible_on, 3, canvas_ptr->height + 39); + drawButton(button_layerVisible, on, 3, canvas_ptr->height + 39); else - drawButton(button_layerVisible_off, 3, canvas_ptr->height + 39); + drawButton(button_layerVisible, off, 3, canvas_ptr->height + 39); SDL_Rect rect; rect.x = 1; rect.y = canvas_ptr->height + 1; @@ -267,14 +300,15 @@ void Bar::draw( void ) SDL_RenderDrawRect(window_ptr->screen_renderer, &rect); } { + //draw terrain layer buttons if (type == TEMP) - drawButton(button_layerTerrain_on, 39, canvas_ptr->height + 3); + drawButton(button_layerTerrain, on, 39, canvas_ptr->height + 3); else - drawButton(button_layerTerrain_off, 39, canvas_ptr->height + 3); + drawButton(button_layerTerrain, off, 39, canvas_ptr->height + 3); if (canvas_ptr->layerVisible[TEMP]) - drawButton(button_layerVisible_on, 39, canvas_ptr->height + 39); + drawButton(button_layerVisible, on, 39, canvas_ptr->height + 39); else - drawButton(button_layerVisible_off, 39, canvas_ptr->height + 39); + drawButton(button_layerVisible, off, 39, canvas_ptr->height + 39); SDL_Rect rect; rect.x = 37; rect.y = canvas_ptr->height + 1; @@ -283,14 +317,15 @@ void Bar::draw( void ) SDL_RenderDrawRect(window_ptr->screen_renderer, &rect); } { + //draw tool layer buttons if (type == TOOL) - drawButton(button_layerTool_on, 75, canvas_ptr->height + 3); + drawButton(button_layerTool, on, 75, canvas_ptr->height + 3); else - drawButton(button_layerTool_off, 75, canvas_ptr->height + 3); + drawButton(button_layerTool, off, 75, canvas_ptr->height + 3); if (canvas_ptr->layerVisible[TOOL]) - drawButton(button_layerVisible_on, 75, canvas_ptr->height + 39); + drawButton(button_layerVisible, on, 75, canvas_ptr->height + 39); else - drawButton(button_layerVisible_off, 75, canvas_ptr->height + 39); + drawButton(button_layerVisible, off, 75, canvas_ptr->height + 39); SDL_Rect rect; rect.x = 73; rect.y = canvas_ptr->height + 1; @@ -299,19 +334,37 @@ void Bar::draw( void ) SDL_RenderDrawRect(window_ptr->screen_renderer, &rect); } - drawButton(button_save_up, 111, canvas_ptr->height + 3); + drawButton(button_save, off, 111, canvas_ptr->height + 3); + drawButton(button_moveToBack, off, 3, canvas_ptr->height + 75); + drawButton(button_moveToFront, off, 39, canvas_ptr->height + 75); + } SDL_SetRenderTarget(window_ptr->screen_renderer, NULL); } -void Bar::drawButton(SDL_Texture * texture, int x, int y) +void Bar::drawButton(const buttonInfo & button, buttonState state, int x, int y) { - SDL_Rect rect; - rect.x = x; - rect.y = y; - rect.w = 32; - rect.h = 32; - SDL_RenderCopy(window_ptr->screen_renderer, texture, NULL, &rect); + SDL_Rect sourceRect; + if (state == off) + { + sourceRect.x = button.texOffX; + sourceRect.y = button.texOffY; + } + if (state == on) + { + sourceRect.x = button.texOnX; + sourceRect.y = button.texOnY; + } + sourceRect.w = 32; + sourceRect.h = 32; + + SDL_Rect destRect; + destRect.x = x; + destRect.y = y; + destRect.w = 32; + destRect.h = 32; + + SDL_RenderCopy(window_ptr->screen_renderer, buttonTexture, &sourceRect, &destRect); } \ No newline at end of file diff --git a/src/Editor/bar.hpp b/src/Editor/bar.hpp index da3697e..53b6b81 100644 --- a/src/Editor/bar.hpp +++ b/src/Editor/bar.hpp @@ -47,20 +47,23 @@ class Bar int type; SDL_Rect barScrollRect; - SDL_Texture * button_layerBackground_off; - SDL_Texture * button_layerBackground_on; - SDL_Texture * button_layerTerrain_off; - SDL_Texture * button_layerTerrain_on; - SDL_Texture * button_layerTool_off; - SDL_Texture * button_layerTool_on; - SDL_Texture * button_layerVisible_off; - SDL_Texture * button_layerVisible_on; - SDL_Texture * button_save_down; - SDL_Texture * button_save_up; + SDL_Texture * buttonTexture; + int currentButtonTextureX; + + struct buttonInfo { int texOnX; int texOnY; int texOffX; int texOffY; }; + enum buttonState { on, off }; + + buttonInfo button_layerBackground; + buttonInfo button_layerTerrain; + buttonInfo button_layerTool; + buttonInfo button_layerVisible; + buttonInfo button_save; + buttonInfo button_moveToBack; + buttonInfo button_moveToFront; void setReferences(Window * w, Editor * e, Canvas * c, Style * s); void load(void); - bool loadButtonGraphic(SDL_Texture *& texture, const char * filePath); + bool loadButtonGraphic(buttonInfo & button, const char * filePathUp, const char * filePathDown); void resizeBarScrollRect(int windowWidth, int windowHeight); void scroll(signed int moveAmount); @@ -73,7 +76,7 @@ class Bar void draw( void ); - void drawButton( SDL_Texture * texture, int x, int y); + void drawButton( const buttonInfo & button, buttonState state, int x, int y); Bar(void) { /* nothing to do */ }; diff --git a/src/Editor/input.cpp b/src/Editor/input.cpp index eef1d1b..bf4ad31 100644 --- a/src/Editor/input.cpp +++ b/src/Editor/input.cpp @@ -253,7 +253,27 @@ void Editor_input::handleEvents(SDL_Event event) } /*if (mouse_x_window > 111 && mouse_x_window < 143) { - editor_ptr->save(level_ptr->level_id); + + }*/ + } + if (mouse_y_window > window_ptr->height - BAR_HEIGHT + 75 && mouse_y_window < window_ptr->height - BAR_HEIGHT + 107) + //third row of buttons + { + if (mouse_x_window > 3 && mouse_x_window < 35) + { + editor_ptr->moveToBack(); + } + if (mouse_x_window > 39 && mouse_x_window < 71) + { + editor_ptr->moveToFront(); + } + /*if (mouse_x_window > 75 && mouse_x_window < 107) + { + + } + if (mouse_x_window > 111 && mouse_x_window < 143) + { + }*/ } } diff --git a/src/style.cpp b/src/style.cpp index 36d4843..500cf84 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -226,8 +226,8 @@ bool Style::load(unsigned int n, Window * window, SDL_Color *pal2) megatex = SDL_CreateTexture(window->screen_renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, 1024, 1024); SDL_SetTextureBlendMode(megatex, SDL_BLENDMODE_BLEND); - SDL_SetTextureAlphaMod(megatex, 0); - SDL_RenderCopy(window->screen_renderer, megatex, NULL, NULL); + //SDL_SetTextureAlphaMod(megatex, 0); + //SDL_RenderCopy(window->screen_renderer, megatex, NULL, NULL); SDL_SetTextureAlphaMod(megatex, 255); megatexAddX = 0; megatexAddY = 0;