diff --git a/src/InputWindowElement.h b/src/InputWindowElement.h index d35d605..15d5d39 100644 --- a/src/InputWindowElement.h +++ b/src/InputWindowElement.h @@ -15,10 +15,12 @@ namespace gj { class InputWindowElement : public iElement { public: struct Param { + iAllocator* alloc; + UniqPtr matcher; UniqPtr driver; - Scoreboard* scoreboard = nullptr; + Scoreboard* scoreboard; Period period; @@ -35,13 +37,14 @@ class InputWindowElement : public iElement { InputWindowElement(Param&& p) : iElement(p.period), matcher_(std::move(p.matcher)), drv_(std::move(p.driver)), sb_(p.scoreboard), - text_(p.text), expects_(matcher_->expects()) { + bg_(CreateEmptySquare_(p.alloc)), text_(p.text), expects_(matcher_->expects()) { param_["posX"] = 0.; param_["posY"] = 0.; param_["baseX"] = .5; } void Update(Frame& frame, double t) override { + /* consumes all input */ for (auto c : frame.input) { if (matcher_->done()) break; if (matcher_->Input(c)) { @@ -50,18 +53,40 @@ class InputWindowElement : public iElement { } ++sb_->input; } + + /* executes the driver to update parameters and takes them with cast */ drv_->Update(param_, t); - const auto posX = (std::get(param_["posX"])+1)/2; - const auto posY = (std::get(param_["posY"])+1)/2; + const auto posX = std::get(param_["posX"]); + const auto posY = std::get(param_["posY"]); const auto baseX = std::get(param_["baseX"]); - const uint32_t posXi = static_cast(posX * frame.w); - const uint32_t posYi = static_cast(posY * frame.h); + const uint32_t posXi = static_cast((posX+1)/2 * frame.w); + const uint32_t posYi = static_cast((posY+1)/2 * frame.h); const uint32_t tbaseXi = static_cast(baseX * text_.width()); const uint32_t ebaseXi = static_cast(baseX * expects_.width()); + const double w = (4+std::max(text_.width(), expects_.width()))*1. / frame.w; + const double h = 4. / frame.h; + + /* calculates geometry of background rect + * and sets alpha to negative to decrease colors of pixels surrounding the text */ + auto M = mat3{ + {w, 0, 0}, + {0, h, 0}, + {0, 0, 1}, + }; + M = ::linalg::mul(mat3{ + {1, 0, 0}, + {0, 1, 0}, + {posX, posY, 1}, + }, M); + bg_.SetMatrix(M); + bg_.SetAlpha(-.8f); + frame.Add(&bg_); + + /* calculates geometry of foreground text */ text_.SetPosition(posXi-tbaseXi, posYi); expects_.SetPosition(posXi-ebaseXi, posYi+1); frame.Add(&text_); @@ -85,10 +110,17 @@ class InputWindowElement : public iElement { Scoreboard* sb_; - Text text_; - Text expects_; + Texture bg_; + Text text_; + Text expects_; iElementDriver::Param param_; + + static Colorbuffer CreateEmptySquare_(iAllocator* alloc) { + auto buf = Colorbuffer(alloc, 1, 1); + buf.ptr()[0] = 1; + return std::move(buf); + } }; diff --git a/src/InputWindowElementFactory.h b/src/InputWindowElementFactory.h index d5a58de..9edf95d 100644 --- a/src/InputWindowElementFactory.h +++ b/src/InputWindowElementFactory.h @@ -29,6 +29,7 @@ class InputWindowElementFactory : public iElementFactory { const std::string kana = std::get(param.custom[1]); InputWindowElement::Param p; + p.alloc = alloc_; p.period = param.period; p.scoreboard = sb_; p.driver = std::move(param.driver);