Skip to content

Commit 16a7448

Browse files
authored
Add controller display options (#6)
option to lock scaling to integer values and popout the display to a window
1 parent adb88be commit 16a7448

File tree

6 files changed

+96
-28
lines changed

6 files changed

+96
-28
lines changed

src/MarlinSimulator/hardware/HD44780Device.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,36 @@ void HD44780Device::ui_init() {
197197
}
198198

199199
void HD44780Device::ui_widget() {
200+
bool popout_begin = false;
200201
static long int call_count = 0;
201202
static uint8_t up_held = 0, down_held = 0;
202203
call_count++;
203204

204205
auto size = ImGui::GetContentRegionAvail();
205-
size.y = size.x / (width / (float)height);
206+
size.y = ((size.x / (width / (float)height)) * !render_popout) + 60;
207+
206208
if (ImGui::BeginChild("HD44780Device", size)) {
207209
ImGui::GetCurrentWindow()->ScrollMax.y = 1.0f; // disable window scroll
210+
ImGui::Checkbox("Integer Scaling", &render_integer_scaling);
211+
ImGui::Checkbox("Popout", &render_popout);
212+
if (render_popout) {
213+
ImGui::SetNextWindowSize(ImVec2(width + 10, height + 10), ImGuiCond_Once);
214+
popout_begin = ImGui::Begin("ST7796DeviceRender", &render_popout);
215+
if (!popout_begin) {
216+
ImGui::End();
217+
return;
218+
}
219+
size = ImGui::GetContentRegionAvail();
220+
}
208221

209-
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
222+
double scale = size.x / width;
223+
if (render_integer_scaling) {
224+
scale = scale > 1.0 ? std::floor(scale) : scale;
225+
size.x = width * scale;
226+
}
227+
size.y = height * scale;
210228

229+
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
211230
if (ImGui::IsWindowFocused()) {
212231

213232
key_pressed[KeyName::KILL_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_K);
@@ -225,6 +244,8 @@ void HD44780Device::ui_widget() {
225244
encoder_position += ImGui::GetIO().MouseWheel > 0 ? 1 : ImGui::GetIO().MouseWheel < 0 ? -1 : 0;
226245
}
227246
}
247+
248+
if (popout_begin) ImGui::End();
228249
}
229250
ImGui::EndChild();
230251
}

src/MarlinSimulator/hardware/HD44780Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class HD44780Device: public VirtualPrinter::Component {
9595
static constexpr uint32_t texture_x = 132;//((5 * display_x_char) + (display_margin * 2) + (19 * display_char_pad));
9696
static constexpr uint32_t texture_y = 48;//((8 * display_y_char) + (display_margin * 2) + (3 * display_char_pad));
9797
static constexpr uint32_t width = texture_x, height = texture_y;
98+
bool render_integer_scaling = false, render_popout = false;
9899

99100
glm::vec<3, uint8_t> texture_data[texture_x * texture_y] = {};
100101

src/MarlinSimulator/hardware/ST7796Device.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,36 @@ void ST7796Device::ui_init() {
127127
}
128128

129129
void ST7796Device::ui_widget() {
130+
bool popout_begin = false;
130131
auto size = ImGui::GetContentRegionAvail();
131-
size.y = size.x / (width / (float)height);
132-
ImGui::BeginChild("ST7796Device", size);
133-
ImGui::GetCurrentWindow()->ScrollMax.y = 1.0f; // disable window scroll
132+
size.y = ((size.x / (width / (float)height)) * !render_popout) + 60;
133+
134+
if (ImGui::BeginChild("ST7796Device", size)) {
135+
ImGui::GetCurrentWindow()->ScrollMax.y = 1.0f; // disable window scroll
136+
ImGui::Checkbox("Integer Scaling", &render_integer_scaling);
137+
ImGui::Checkbox("Popout", &render_popout);
138+
139+
if (render_popout) {
140+
ImGui::SetNextWindowSize(ImVec2(width + 10, height + 10), ImGuiCond_Once);
141+
popout_begin = ImGui::Begin("ST7796DeviceRender", &render_popout);
142+
if (!popout_begin) {
143+
ImGui::End();
144+
return;
145+
}
146+
size = ImGui::GetContentRegionAvail();
147+
}
134148

135-
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
136-
//if (ImGui::IsItemFocused()) {
149+
double scale = size.x / width;
150+
if (render_integer_scaling) {
151+
scale = scale > 1.0 ? std::floor(scale) : scale;
152+
size.x = width * scale;
153+
}
154+
size.y = height * scale;
155+
156+
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
137157
touch->ui_callback();
138-
//}
139158

159+
if (popout_begin) ImGui::End();
160+
}
140161
ImGui::EndChild();
141162
}

src/MarlinSimulator/hardware/ST7796Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ST7796Device: public SPISlavePeripheral {
4343
void onEndTransaction() override;
4444

4545
static constexpr uint32_t width = TFT_WIDTH, height = TFT_HEIGHT;
46+
bool render_integer_scaling = false, render_popout = false;
4647

4748
pin_type dc_pin, beeper_pin, enc1_pin, enc2_pin, enc_but_pin, back_pin, kill_pin;
4849

src/MarlinSimulator/hardware/ST7920Device.cpp

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,52 @@ void ST7920Device::ui_widget() {
154154
static long int call_count = 0;
155155
static uint8_t up_held = 0, down_held = 0;
156156
call_count++;
157+
bool popout_begin = false;
157158

158159
auto size = ImGui::GetContentRegionAvail();
159-
size.y = size.x / (width / (float)height);
160-
ImGui::BeginChild("ST7920Device", size);
161-
ImGui::GetCurrentWindow()->ScrollMax.y = 1.0f; // disable window scroll
162-
163-
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
164-
165-
if (ImGui::IsWindowFocused()) {
166-
key_pressed[KeyName::KILL_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_K);
167-
key_pressed[KeyName::ENCODER_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_SPACE) || ImGui::IsKeyDown(SDL_SCANCODE_RETURN) || ImGui::IsKeyDown(SDL_SCANCODE_RIGHT);
168-
key_pressed[KeyName::BACK_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_LEFT);
169-
170-
// Turn keypresses (and repeat) into encoder clicks
171-
if (up_held) { up_held--; encoder_position--; }
172-
else if (ImGui::IsKeyPressed(SDL_SCANCODE_UP)) up_held = 4;
173-
if (down_held) { down_held--; encoder_position++; }
174-
else if (ImGui::IsKeyPressed(SDL_SCANCODE_DOWN)) down_held = 4;
175-
176-
if (ImGui::IsItemHovered()) {
177-
key_pressed[KeyName::ENCODER_BUTTON] |= ImGui::IsMouseClicked(0);
178-
encoder_position += ImGui::GetIO().MouseWheel > 0 ? 1 : ImGui::GetIO().MouseWheel < 0 ? -1 : 0;
160+
size.y = ((size.x / (width / (float)height)) * !render_popout) + 60;
161+
162+
if (ImGui::BeginChild("ST7920Device", size)) {
163+
ImGui::GetCurrentWindow()->ScrollMax.y = 1.0f; // disable window scroll
164+
ImGui::Checkbox("Integer Scaling", &render_integer_scaling);
165+
ImGui::Checkbox("Popout", &render_popout);
166+
167+
if (render_popout) {
168+
ImGui::SetNextWindowSize(ImVec2(width + 10, height + 10), ImGuiCond_Once);
169+
popout_begin = ImGui::Begin("ST7920DeviceRender", &render_popout);
170+
if (!popout_begin) {
171+
ImGui::End();
172+
return;
173+
}
174+
size = ImGui::GetContentRegionAvail();
175+
}
176+
177+
double scale = size.x / width;
178+
if (render_integer_scaling) {
179+
scale = scale > 1.0 ? std::floor(scale) : scale;
180+
size.x = width * scale;
179181
}
182+
size.y = height * scale;
183+
184+
ImGui::Image((ImTextureID)(intptr_t)texture_id, size, ImVec2(0,0), ImVec2(1,1));
185+
if (ImGui::IsWindowFocused()) {
186+
key_pressed[KeyName::KILL_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_K);
187+
key_pressed[KeyName::ENCODER_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_SPACE) || ImGui::IsKeyDown(SDL_SCANCODE_RETURN) || ImGui::IsKeyDown(SDL_SCANCODE_RIGHT);
188+
key_pressed[KeyName::BACK_BUTTON] = ImGui::IsKeyDown(SDL_SCANCODE_LEFT);
189+
190+
// Turn keypresses (and repeat) into encoder clicks
191+
if (up_held) { up_held--; encoder_position--; }
192+
else if (ImGui::IsKeyPressed(SDL_SCANCODE_UP)) up_held = 4;
193+
if (down_held) { down_held--; encoder_position++; }
194+
else if (ImGui::IsKeyPressed(SDL_SCANCODE_DOWN)) down_held = 4;
195+
196+
if (ImGui::IsItemHovered()) {
197+
key_pressed[KeyName::ENCODER_BUTTON] |= ImGui::IsMouseClicked(0);
198+
encoder_position += ImGui::GetIO().MouseWheel > 0 ? 1 : ImGui::GetIO().MouseWheel < 0 ? -1 : 0;
199+
}
200+
}
201+
202+
if (popout_begin) ImGui::End();
180203
}
181204
ImGui::EndChild();
182205
}

src/MarlinSimulator/hardware/ST7920Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class ST7920Device: public VirtualPrinter::Component {
3333
void ui_widget() override;
3434

3535
static constexpr uint32_t width = 128, height = 64;
36+
bool render_integer_scaling = false, render_popout = false;
3637

3738
pin_type clk_pin, mosi_pin, cs_pin, beeper_pin, enc1_pin, enc2_pin, enc_but_pin, back_pin, kill_pin;
3839

0 commit comments

Comments
 (0)