1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#include "main.h"
19
20#include "settings.h"
21
22#include "dcbutton.h"
23#include "sprites.h"
24#include "gui.h"
25
26BEGIN_EVENT_TABLE(DCButton, wxPanel)
27EVT_PAINT(DCButton::OnPaint)
28EVT_LEFT_DOWN(DCButton::OnClick)
29END_EVENT_TABLE()
30
31IMPLEMENT_DYNAMIC_CLASS(DCButton, wxPanel)
32
33DCButton::DCButton() :
34 wxPanel(nullptr, wxID_ANY, wxDefaultPosition, wxSize(36, 36)),
35 type(DC_BTN_NORMAL),
36 state(false),
37 size(RENDER_SIZE_16x16),
38 sprite(nullptr),
39 overlay(nullptr) {
40 SetSprite(0);
41}
42
43DCButton::DCButton(wxWindow* parent, wxWindowID id, wxPoint pos, int type, RenderSize sz, int sprite_id) :
44 wxPanel(parent, id, pos, (sz == RENDER_SIZE_64x64 ? wxSize(68, 68) : sz == RENDER_SIZE_32x32 ? wxSize(36, 36)
45 : wxSize(20, 20))),
46 type(type),
47 state(false),
48 size(sz),
49 sprite(nullptr),
50 overlay(nullptr) {
51 SetSprite(sprite_id);
52}
53
54DCButton::~DCButton() {
55 ////
56}
57
58void DCButton::SetSprite(int _sprid) {
59 if (_sprid != 0) {
60 sprite = g_gui.gfx.getSprite(_sprid);
61 } else {
62 sprite = nullptr;
63 }
64 Refresh();
65}
66
67void DCButton::SetOverlay(Sprite* espr) {
68 overlay = espr;
69 Refresh();
70}
71
72void DCButton::SetValue(bool val) {
73 ASSERT(type == DC_BTN_TOGGLE);
74 bool oldval = val;
75 state = val;
76 if (state == oldval) {
77 // Cheap to change value to the old one (which is done ALOT)
78 if (GetValue() && g_settings.getInteger(Config::USE_GUI_SELECTION_SHADOW)) {
79 SetOverlay(g_gui.gfx.getSprite(EDITOR_SPRITE_SELECTION_MARKER));
80 } else {
81 SetOverlay(nullptr);
82 }
83 Refresh();
84 }
85}
86
87bool DCButton::GetValue() const {
88 ASSERT(type == DC_BTN_TOGGLE);
89 return state;
90}
91
92void DCButton::OnPaint(wxPaintEvent& event) {
93 wxBufferedPaintDC pdc(this);
94
95 if (g_gui.gfx.isUnloaded()) {
96 return;
97 }
98
99 static std::unique_ptr<wxPen> highlight_pen;
100 static std::unique_ptr<wxPen> dark_highlight_pen;
101 static std::unique_ptr<wxPen> light_shadow_pen;
102 static std::unique_ptr<wxPen> shadow_pen;
103
104 if (highlight_pen.get() == nullptr) {
105 highlight_pen.reset(newd wxPen(wxColor(0xFF, 0xFF, 0xFF), 1, wxSOLID));
106 }
107 if (dark_highlight_pen.get() == nullptr) {
108 dark_highlight_pen.reset(newd wxPen(wxColor(0xD4, 0xD0, 0xC8), 1, wxSOLID));
109 }
110 if (light_shadow_pen.get() == nullptr) {
111 light_shadow_pen.reset(newd wxPen(wxColor(0x80, 0x80, 0x80), 1, wxSOLID));
112 }
113 if (shadow_pen.get() == nullptr) {
114 shadow_pen.reset(newd wxPen(wxColor(0x40, 0x40, 0x40), 1, wxSOLID));
115 }
116
117 int size_x = 20, size_y = 20;
118
119 if (size == RENDER_SIZE_16x16) {
120 size_x = 20;
121 size_y = 20;
122 } else if (size == RENDER_SIZE_32x32) {
123 size_x = 36;
124 size_y = 36;
125 }
126
127 pdc.SetBrush(*wxBLACK);
128 pdc.DrawRectangle(0, 0, size_x, size_y);
129 if (type == DC_BTN_TOGGLE && GetValue()) {
130 pdc.SetPen(*shadow_pen);
131 pdc.DrawLine(0, 0, size_x - 1, 0);
132 pdc.DrawLine(0, 1, 0, size_y - 1);
133 pdc.SetPen(*light_shadow_pen);
134 pdc.DrawLine(1, 1, size_x - 2, 1);
135 pdc.DrawLine(1, 2, 1, size_y - 2);
136 pdc.SetPen(*dark_highlight_pen);
137 pdc.DrawLine(size_x - 2, 1, size_x - 2, size_y - 2);
138 pdc.DrawLine(1, size_y - 2, size_x - 1, size_y - 2);
139 pdc.SetPen(*highlight_pen);
140 pdc.DrawLine(size_x - 1, 0, size_x - 1, size_y - 1);
141 pdc.DrawLine(0, size_y - 1, size_y, size_y - 1);
142 } else {
143 pdc.SetPen(*highlight_pen);
144 pdc.DrawLine(0, 0, size_x - 1, 0);
145 pdc.DrawLine(0, 1, 0, size_y - 1);
146 pdc.SetPen(*dark_highlight_pen);
147 pdc.DrawLine(1, 1, size_x - 2, 1);
148 pdc.DrawLine(1, 2, 1, size_y - 2);
149 pdc.SetPen(*light_shadow_pen);
150 pdc.DrawLine(size_x - 2, 1, size_x - 2, size_y - 2);
151 pdc.DrawLine(1, size_y - 2, size_x - 1, size_y - 2);
152 pdc.SetPen(*shadow_pen);
153 pdc.DrawLine(size_x - 1, 0, size_x - 1, size_y - 1);
154 pdc.DrawLine(0, size_y - 1, size_y, size_y - 1);
155 }
156
157 if (sprite) {
158 if (size == RENDER_SIZE_16x16) {
159 // Draw the picture!
160 sprite->DrawTo(&pdc, SPRITE_SIZE_16x16, 2, 2);
161
162 if (overlay && type == DC_BTN_TOGGLE && GetValue()) {
163 overlay->DrawTo(&pdc, SPRITE_SIZE_16x16, 2, 2);
164 }
165 } else if (size == RENDER_SIZE_32x32) {
166 // Draw the picture!
167 sprite->DrawTo(&pdc, SPRITE_SIZE_32x32, 2, 2);
168
169 if (overlay && type == DC_BTN_TOGGLE && GetValue()) {
170 overlay->DrawTo(&pdc, SPRITE_SIZE_32x32, 2, 2);
171 }
172 } else if (size == RENDER_SIZE_64x64) {
173 ////
174 }
175 }
176}
177
178void DCButton::OnClick(wxMouseEvent& WXUNUSED(evt)) {
179 wxCommandEvent event(type == DC_BTN_TOGGLE ? wxEVT_COMMAND_TOGGLEBUTTON_CLICKED : wxEVT_COMMAND_BUTTON_CLICKED, GetId());
180 event.SetEventObject(this);
181
182 if (type == DC_BTN_TOGGLE) {
183 SetValue(!GetValue());
184 }
185 SetFocus();
186
187 GetEventHandler()->ProcessEvent(event);
188}
189