From bc56befc8ec2dedf476897321db8a95fb5a6aa97 Mon Sep 17 00:00:00 2001 From: falsycat Date: Thu, 26 Aug 2021 15:05:41 +0900 Subject: [PATCH] Adds baseX property for InputWindowElement. --- src/Frame.h | 4 +++- src/Game.cc | 2 +- src/InputWindowElement.h | 22 +++++++++++++++------- src/TextureElement.h | 38 +++++++++++++++++++++++++++++++++----- src/common.h | 8 ++++++++ 5 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/Frame.h b/src/Frame.h index 3709b79..c992237 100644 --- a/src/Frame.h +++ b/src/Frame.h @@ -19,7 +19,7 @@ class Frame : public iDrawable, public iWritable { Frame& operator=(Frame&&) = delete; Frame& operator=(const Frame&) = delete; - Frame(size_t dres, size_t wres) { + Frame(uint32_t w, uint32_t h, size_t dres, size_t wres) : w(w), h(h) { draw_.reserve(dres); write_.reserve(wres); } @@ -49,6 +49,8 @@ class Frame : public iDrawable, public iWritable { std::string input; + uint32_t w, h; + private: std::vector draw_; std::vector write_; diff --git a/src/Game.cc b/src/Game.cc index 7e999e1..e26b09e 100644 --- a/src/Game.cc +++ b/src/Game.cc @@ -7,7 +7,7 @@ gj::Game::Game(gj::Game::Param&& p) : clock_(p.clock), logger_(p.h), w_(p.w), h_(p.h), - frame_(kReserveDrawable, kReserveWritable) { + frame_(p.w, p.h, kReserveDrawable, kReserveWritable) { gj::PlayScene::Param param; param.alloc = alloc_; param.clock = &clock_; diff --git a/src/InputWindowElement.h b/src/InputWindowElement.h index 55211fb..792a3ec 100644 --- a/src/InputWindowElement.h +++ b/src/InputWindowElement.h @@ -32,9 +32,10 @@ class InputWindowElement : public iElement { InputWindowElement(Param&& p) : iElement(p.period), matcher_(std::move(p.matcher)), drv_(std::move(p.driver)), - text_(p.text), guide_(matcher_->expects()) { - param_["posX"] = 0; - param_["posY"] = 0; + text_(p.text), guide_(matcher_->expects()), width_(CountWstrBytes(p.text)) { + param_["posX"] = 0.; + param_["posY"] = 0.; + param_["baseX"] = .5; } void Update(Frame& frame, double t) override { @@ -46,11 +47,16 @@ class InputWindowElement : public iElement { } drv_->Update(param_, t); - const auto posX = std::get(param_["posX"]); - const auto posY = std::get(param_["posY"]); + const auto posX = (std::get(param_["posX"])+1)/2; + const auto posY = (std::get(param_["posY"])+1)/2; + const auto baseX = std::get(param_["baseX"]); - text_.SetPosition(posX, posY); - guide_.SetPosition(posX, posY+1); + const uint32_t posXi = static_cast(posX * frame.w); + const uint32_t posYi = static_cast(posY * frame.h); + const uint32_t baseXi = static_cast(baseX * width_); + + text_.SetPosition(posXi-baseXi, posYi); + guide_.SetPosition(posXi-baseXi, posYi+1); frame.Add(&text_); frame.Add(&guide_); } @@ -66,6 +72,8 @@ class InputWindowElement : public iElement { Text text_; Text guide_; + size_t width_; + iElementDriver::Param param_; }; diff --git a/src/TextureElement.h b/src/TextureElement.h index 58e2362..0166611 100644 --- a/src/TextureElement.h +++ b/src/TextureElement.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "iElement.h" #include "iElementDriver.h" #include "Texture.h" @@ -22,6 +24,7 @@ public: param_["posY"] = 0.; param_["scaleX"] = 1.; param_["scaleY"] = 1.; + param_["rota"] = 0.; param_["alpha"] = 1.; } @@ -32,13 +35,38 @@ public: const double posY = std::get(param_["posY"]); const double scaleX = std::get(param_["scaleX"]); const double scaleY = std::get(param_["scaleY"]); + const double rota = std::get(param_["rota"]); const double alpha = std::get(param_["alpha"]); - tex_.SetMatrix(mat3{ - { scaleX, 0, 0 }, - { 0, scaleY, 0 }, - { posX, posY, 1}, - }); + const double c = std::cos(rota); + const double s = std::sin(rota); + + const double aspect = frame.w*1./frame.h; + auto Ms = mat3{ + { scaleX, 0, 0 }, + { 0, scaleY, 0 }, + { 0, 0, 1}, + }; + auto Mr = mat3{ + { c, -s, 0, }, + { s, c, 0, }, + { 0, 0, 1, }, + }; + auto Mm = mat3{ + { 1, 0, 0 }, + { 0, 1, 0 }, + { posX, posY, 1}, + }; + auto M = mat3{ + { 1, 0, 0 }, + { 0, aspect/2, 0 }, + { 0, 0, 1}, + }; + M = ::linalg::mul(M, Mm); + M = ::linalg::mul(M, Mr); + M = ::linalg::mul(M, Ms); + + tex_.SetMatrix(M); tex_.SetAlpha(static_cast(alpha)); frame.Add(&tex_); diff --git a/src/common.h b/src/common.h index c09a6db..c4edbbf 100644 --- a/src/common.h +++ b/src/common.h @@ -30,6 +30,14 @@ static inline std::wstring ConvertStrToWstr(const std::string& str) { } } +static inline size_t CountWstrBytes(const std::wstring& str) { + size_t n = 0; + for (const auto c : str) { + n += c > UINT8_MAX? 2: 1; + } + return n; +} + [[noreturn]] static inline void Abort(const std::string& msg) {