Bring in the components into separate files so I can start to see how to make them generic. Then make the calculator kind of work but not yet.

This commit is contained in:
Zed A. Shaw 2025-05-06 00:22:03 -04:00
parent 313a9aec83
commit 4b07ecac45
3 changed files with 330 additions and 5 deletions

View file

@ -4,17 +4,31 @@
#include "sfml/textures.hpp"
#include "guecs.hpp"
#include "constants.hpp"
#include <fmt/xchar.h>
constexpr const int WINDOW_WIDTH=300;
constexpr const int WINDOW_HEIGHT=400;
using std::string, std::wstring;
const std::unordered_map<string, wstring> LABELS {
{"readout", L""}, {"clear", L"CLR"}, {"btn0", L"0"}, {"btn1", L"1"},
{"btn2", L"2"}, {"btn3", L"3"}, {"btn4", L"4"},
{"btn5", L"5"}, {"btn6", L"6"}, {"btn7", L"7"},
{"btn8", L"8"}, {"btn9", L"9"}, {"mult", L"*"},
{"minus", L"-"}, {"plus", L"+"}, {"neg", L"!"},
{"dot", L"."}, {"eq", L"="}
};
struct Calculator {
guecs::UI $gui;
wstring $input;
double $value = 0.0;
Calculator() {
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
$gui.layout(
"[readout]"
"[*%(400)readout|_|_|_|clear]"
"[btn7|btn8|btn9|mult]"
"[btn4|btn5|btn6|minus]"
"[btn1|btn2|btn3|plus]"
@ -26,12 +40,19 @@ struct Calculator {
for(auto& [name, cell] : $gui.cells()) {
auto id = $gui.entity(name);
auto& label = LABELS.at(name);
$gui.set<guecs::Rectangle>(id, {});
$gui.set<guecs::Effect>(id, {});
$gui.set<guecs::Label>(id, {guecs::to_wstring(name)});
$gui.set<guecs::Clickable>(id, {
[=](auto, auto) { fmt::println("clicked {}", name); }
});
if(name == "readout") {
$gui.set<guecs::Textual>(id, {L"", 40});
} else {
$gui.set<guecs::Label>(id, { label });
$gui.set<guecs::Clickable>(id, {
[&, name](auto, auto) { handle_button(label[0]); }
});
}
}
$gui.init();
@ -45,9 +66,57 @@ struct Calculator {
void mouse(float x, float y, bool hover) {
$gui.mouse(x, y, hover);
}
void handle_button(wchar_t op) {
switch(op) {
case L'0':
case L'1':
case L'2':
case L'3':
case L'4':
case L'5':
case L'6':
case L'7':
case L'8':
case L'9':
case L'.':
if($input.size() <= 10) {
$input += op;
}
break;
case L'*':
$value = $value * std::stof($input);
$input = L"";
break;
case L'-':
$value = $value - std::stof($input);
$input = L"";
break;
case L'+':
$value = $value + std::stof($input);
$input = L"";
break;
case L'!':
$value = $value * -1.0;
$input = L"";
break;
case L'=':
$input = fmt::format(L"{}", $value);
$value = 0.0;
break;
case L'C':
$input = L"";
break;
}
auto readout = $gui.entity("readout");
auto& label = $gui.get<guecs::Textual>(readout);
label.update($input);
}
};
int main() {
sound::init();
shaders::init();