// SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "ui/strengths.hpp" #include #include #include "ui/manager.hpp" namespace bmh::ui { namespace { void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx<0||sy<0||x<0||y<0||w<0||h<0||sx+w>src.width||sy+h>src.height||x+w>out.width||y+h>out.height) throw FormatError("strengths: Original atlas bounds"); for(int yy=0;yy3||id.league<0||id.league>8||!id.scope|| p.manager_slot!=id.manager||p.league!=id.league||state_.league_group<0||state_.league_group>1|| id.expected_settings_word!=p.words.post_draw_settings_word|| p.rows.size()!=size_t(exe_.u8(::bmh::layout::LeagueSizes+uint32_t(id.league)))||p.age_scratch.league!=id.league|| p.age_scratch.tenths.size()!=p.rows.size()||state_.title.empty()||state_.subtitle.empty()) throw FormatError("strengths: incomplete owned projection"); (void)render(); } void StrengthsScreen::text(Image& out,int x,int baseline,std::string_view bytes,uint8_t color) const { const int width=font_.text_width(bytes);if(x<0||x+width>out.width||baseline<5||baseline>=out.height)return; Image mask(std::max(1,width),font_.height);font_.draw(mask,0,0,bytes,1); int index=0;while(index<32&&exe_.u8(::bmh::layout::Advertising_Text_RuntimeField2+uint32_t(index))!=color)++index; for(int yy=0;yy20) {const int n=std::min(8,width-used-20);copy(out,widgets_,24,sy+10,dx+used,dy,n,10);used+=n;} const int left=width-used;copy(out,widgets_,45-left,sy+20,dx+used,dy,left,10); const auto utf8=original_to_utf8(std::string(label)); sprite_.draw(out,dx+width/2-sprite_.text_width(utf8)/2,dy+7,utf8,exe_.u8(::bmh::layout::Advertising_Button_RuntimeField + uint32_t(selected))); } void StrengthsScreen::selector(Image& out,int x,int y,int style) const { const int height=exe_.u8(::bmh::layout::Strengths_Selector_RuntimeField);copy(out,arrows_,198,0,x,y,55,38); copy(out,arrows_,198,76-height,x,y+37,55,height-38); if(style<3)copy(out,arrows_,exe_.u8(::bmh::layout::LeaguePairings_Selector_RuntimeField3+uint32_t(style)),0,x+1,y+exe_.u8(::bmh::layout::Strengths_Selector_RuntimeField2), exe_.u8(::bmh::layout::LeaguePairings_Selector_RuntimeField4+uint32_t(style)),38); } Image StrengthsScreen::render() const { Image out=state_.decorated?frame_:Image(320,240); if(!state_.decorated) {fill(out,0,4,320,13,0x44);fill(out,1,5,318,11,0x39);fill(out,0,228,320,10,0x44);fill(out,1,229,318,8,0x39);} const auto title=original_to_utf8(state_.title);sprite_.draw(out,SpriteFont::centered_title_x(sprite_.text_width(title)),13,title); sprite_.draw(out,5,235,original_to_utf8(state_.subtitle)); const int step=exe_.u8(::bmh::layout::Strengths_PointerDown_RuntimeField+uint32_t(state_.identity.league)),bottom=step*int(state_.projection.rows.size())+27; if(state_.original_panel)copy(out,panel_,0,7,1,27,318,bottom-27); else fill(out,0,26,320,bottom-25,0x42); fill(out,0,26,320,1,0x46);fill(out,0,26,1,bottom-25,0x46); fill(out,0,bottom,320,1,0x40);fill(out,319,26,1,bottom-25,0x40); const uint16_t word=state_.projection.words.post_draw_settings_word; unsigned emphasis=0; if(word&0x200) {copy(out,arrows_,188,16,exe_.u16(::bmh::layout::Strengths_Fill_RuntimeField3+10),18,10,8);emphasis=0x20;} else if((word&0xff)==0xff) {copy(out,arrows_,188,16,exe_.u16(::bmh::layout::Strengths_Fill_RuntimeField3+8),18,10,8);emphasis=0x10;} else for(int n=0;n<4;++n)if(word&(1u<1)aggregate[1]=char(0x7f); aggregate+=number(row.aggregate_value,2)+exe_.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer11); text(out,205,y,aggregate,(emphasis&0x10)?0x6c:0x68); auto age=texts_[938];if(!age.empty())age[0]=char(0x7f); age+=number(unsigned(row.age_tenths)/10+16,2)+exe_.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer2)+number(unsigned(row.age_tenths)%10,1)+texts_[939]; text(out,236,y,age,(emphasis&0x20)?0x6c:0x68); } for(int n=0;n<12;++n)button(out,1+n/4*65,176+n%4*13,63,texts_[103+size_t(n)],pressed_==n||(word&(1u<>15)],pressed_==12); selector(out,205,176,3);selector(out,262,176,state_.league_group); int league=state_.league_group*5; for(int r=0;r<4;++r) {const int count=exe_.u8(::bmh::layout::CalendarLeagueCounts+uint32_t(state_.league_group*4+r)); for(int n=0;n=bx&&x<=bx+63&&y>=by&&y<=by+10)return n;} return x>=202&&x<=316&&y>=215&&y<=225?12:-1; } std::optional StrengthsScreen::selector_at(int x,int y) const { if(x>=205&&x<=260&&y>=176&&y<=216) { auto image=render();if(image.at(x,y)==0x19||image.at(x,y)==0x42)return {}; const int r=(y-exe_.u8(::bmh::layout::Strengths_SelectorAt_RuntimeField))/exe_.u8(::bmh::layout::Strengths_SelectorAt_RuntimeField2);if(r<0||r>=4)return {}; int column=0,league; if(state_.league_group==0) { const int count=exe_.u8(::bmh::layout::CalendarLeagueCounts+uint32_t(r));int left=233-count*4; while(columnleft+8)) {++column;left+=8;} league=exe_.u8(::bmh::layout::CalendarFirstLeague+uint32_t(r))+column; } else { column=(exe_.u8(::bmh::layout::LeaguePairings_SelectorEvent_RuntimeField + uint32_t(r))*4+x-233)/8; league=exe_.u8(::bmh::layout::CareerSession_FormatError_RuntimeField4)+r+column; } if(league>=0&&league<9&&league!=state_.identity.league) return StrengthsRequest{state_.identity,StrengthsAction::select_league,StrengthsControl::condition,league}; } if(x>=262&&x<=317&&y>=176&&y<=216&&state_.secondary_leagues) return StrengthsRequest{state_.identity,StrengthsAction::toggle_group}; return {}; } void StrengthsScreen::post(StrengthsAction kind,int value,StrengthsControl control) { pending_=StrengthsRequest{state_.identity,kind,control,value};rejection_.clear(); } bool StrengthsScreen::pointer_down(int x,int y,bool right) { if(pending_)return false; if(right) {post(StrengthsAction::close);return true;} if(auto selector=selector_at(x,y)) {pending_=*selector;return true;} const int step=exe_.u8(::bmh::layout::Strengths_PointerDown_RuntimeField+uint32_t(state_.identity.league)); if(y>23&&y=0; } bool StrengthsScreen::move(int x,int y) {if(pressed_>=0&&button_at(x,y)!=pressed_) {pressed_=-1;return true;}return false;} bool StrengthsScreen::release(int x,int y) { if(pending_)return false; const int b=pressed_;pressed_=-1; if(b<0||button_at(x,y)!=b)return b>=0; if(b==10||b==11)post(b==10?StrengthsAction::pairings:StrengthsAction::table); else post(StrengthsAction::control,-1,static_cast(b)); return true; } void StrengthsScreen::reject_action(std::string reason) { if(!pending_||reason.empty())throw FormatError("strengths: no pending rejection"); pending_.reset();pressed_=-1;rejection_=std::move(reason); } std::string StrengthsScreen::state_json() const { std::ostringstream s;const auto& id=state_.identity;s<<"{\"screen\":\"strengths\",\"manager\":"<