// SPDX-License-Identifier: GPL-3.0-only namespace { std::optional stadium_integer(const std::string &text,int maximum) { const auto dot=text.find('.');const auto whole=text.substr(0,dot); if(whole.empty()||whole.size()>8||std::any_of(whole.begin(),whole.end(),[](char c){return c<'0'||c>'9';}))return {}; if(dot!=std::string::npos&&(dot+1==text.size()||std::any_of(text.begin()+std::ptrdiff_t(dot+1),text.end(),[](char c){return c!='0';})))return {}; const auto value=std::stoll(whole);return value<=maximum?std::optional(int(value)):std::optional(); } } // namespace void App::sync_stadium_draft() { if(engine_mode_){if(auto *button=element("stadium-build"))button->SetAttribute("disabled",true);for(const auto *id:{"stadium-quote-total","stadium-cost-placesCost","stadium-cost-seatsCost","stadium-cost-roofCost"})if(auto *n=element(id))n->SetInnerRML("—");if(auto *n=element("stadium-draft-feedback"))n->SetInnerRML("Angebot für die geänderte Auswahl wird aktualisiert.");if(auto *n=element("stadium-place-number"))n->SetInnerRML(""+std::to_string(stadium_view_.at("draft").at("places").get())+"");dirty_=true;return;} stadium_view_defaults(stadium_view_);if(stadium_view_.at("dialog")==""||!stadium_view_.at("draft").is_object())return; const auto s=snap();const bool fresh=s.world==stadium_world_&&source_.get()==stadium_source_&&s.revision==stadium_revision_&&stadium_managed(s)==stadium_view_.value("bound_club",std::string()); const auto q=fresh?demo_stadium_quote(s.state,stadium_view_.at("draft"),*s.world,stadium_managed(s)):Json{{"ok",false}}; auto feedback=stadium_view_.at("feedback").get(); if(!fresh)feedback="Der Stadionstand hat sich geändert. Bitte den Ausbau erneut öffnen."; if(feedback.empty())feedback=stadium_feedback(q,stadium_view_.at("invalid_places").get()); if(auto *n=element("stadium-draft-feedback"))n->SetInnerRML(esc(feedback)); if(auto *n=element("stadium-quote-total"))n->SetInnerRML(s.world?esc(stadium_money(stadium_field(q,"cost"),*s.world)):"—"); for(const auto *key:{"placesCost","seatsCost","roofCost"})if(auto *n=element("stadium-cost-"+std::string(key)))n->SetInnerRML(s.world?esc(stadium_money(stadium_field(q,key),*s.world)):"—"); if(auto *button=element("stadium-build")) { if(!fresh||!q.value("can_build",false)||stadium_view_.at("invalid_places").get())button->SetAttribute("disabled",true); else button->RemoveAttribute("disabled"); } if(stadium_view_.at("dialog")=="block")if(auto *button=element("stadium-place-number"))button->SetInnerRML(""+std::to_string(stadium_view_.at("draft").at("places").get())+""); dirty_=true; } // Choosing a part of the model, and laying the view back to where it started. bool App::stadium_focus_action(const std::string &action,Rml::Element *target) { if(action=="stadium-reset") { stadium_view_defaults(stadium_view_); stadium_cams_.erase(stadium_view_.at("view")=="panorama"?"panorama":"inside"); focus_after_render_="stadium-reset";refresh();return true; } if((action!="stadium-focus"&&action!="stadium-pick")||!target)return false; // the click that ends a drag of the view chooses nothing if(stadium_dragged_){stadium_dragged_=false;return true;} // a click on the stage chooses what lies under the pointer, or lets go of the chosen part const auto next=action=="stadium-pick"?stadium_hover_:std::string(target->GetAttribute("data-id","")); if(next==stadium_focus_)return true; stadium_focus_=next; focus_after_render_=next.find(':')!=std::string::npos?"stadium-card-build":""; refresh(); return true; } // The camera on its way: slow at both ends, only the place of the picture changes. void App::stadium_flight_tick() { if(menu_!="stadium"){stadium_seen_.clear();stadium_flying_=false;return;} if(!stadium_flying_)return; auto *world=element("stadium-world"); if(!world){stadium_flying_=false;return;} const double t=std::min(1.0,double(SDL_GetTicks64()-stadium_flight_start_)/700.0),eased=t<.5?4*t*t*t:1-std::pow(-2*t+2,3)/2; for(size_t i=0;i<3;++i)stadium_now_[i]=stadium_from_[i]+(stadium_to_[i]-stadium_from_[i])*eased; if(t>=1){stadium_now_=stadium_to_;stadium_flying_=false;} world->SetProperty("left",pg(stadium_now_[1]));world->SetProperty("top",pg(stadium_now_[2]));world->SetProperty("transform","scale("+scalar(stadium_now_[0])+")"); dirty_=true; } // Looking around on the stage: the wheel zooms towards the pointer, dragging moves the view, // and the pointer over a part shows what stands there. Only the place of the picture changes; // nothing is drawn anew. bool App::stadium_stage_event(Rml::Event &event) { auto *stage=element("stadium-stage");auto *world=element("stadium-world"); if(!stage||!world)return false; const auto type=event.GetType(); if(type!="mousemove"&&type!="mousedown"&&type!="mouseup"&&type!="mousescroll")return false; // while the camera flies the picture is not where the pointer would look for it if(stadium_flying_)return false; const bool outer=stadium_view_.value("view",std::string())=="panorama"; // the pointer on the stage, in units of the page const double mx=(event.GetParameter("mouse_x",0)-stage->GetAbsoluteLeft())/scale_/2.25,my=(event.GetParameter("mouse_y",0)-stage->GetAbsoluteTop())/scale_/2.25; const bool on_world=event.GetTargetElement()&&event.GetTargetElement()->GetId()=="stadium-model-ground"; // the part under the pointer: the one in front wins const auto under=[&](const std::array &cam)->std::string { if(!on_world)return {}; const double x=(mx-cam[1])/cam[0],y=(my-cam[2])/cam[0];std::string found; for(const auto &part:stadium_hots_) for(const auto &quad:part.quads) { const auto a=stadium_scene::project(quad.a),b=stadium_scene::project(quad.b),d=stadium_scene::project(quad.d); const double ux=b.x-a.x,uy=b.y-a.y,vx=d.x-a.x,vy=d.y-a.y,det=ux*vy-uy*vx; if(std::abs(det)<1e-6)continue; const double s=((x-a.x)*vy-(y-a.y)*vx)/det,t=(ux*(y-a.y)-uy*(x-a.x))/det; if(s>=0&&s<=1&&t>=0&&t<=1){found=part.id;break;} } return found; }; const auto part_element=[&](const std::string &id){return id.empty()?nullptr:element("stadium-part-"+id.substr(0,id.find(':'))+"-"+id.substr(id.find(':')+1));}; const auto hover=[&](const std::string &id) { if(id==stadium_hover_)return; if(auto *old=part_element(stadium_hover_))old->SetClass("hover",false); if(auto *now=part_element(id))now->SetClass("hover",true); stadium_hover_=id;dirty_=true; }; const auto place=[&](std::array cam) { constexpr double w=stadium_scene::width,h=stadium_scene::height; cam[0]=std::clamp(cam[0],.75,3.2); cam[1]=std::clamp(cam[1],std::min(240.0,w-w*cam[0]-240),std::max(240.0,w-w*cam[0]-240)); cam[2]=std::clamp(cam[2],std::min(240.0,h-h*cam[0]-240),std::max(240.0,h-h*cam[0]-240)); stadium_cams_[outer?"panorama":"inside"]=cam;stadium_now_=cam; world->SetProperty("left",pg(cam[1]));world->SetProperty("top",pg(cam[2]));world->SetProperty("transform","scale("+scalar(cam[0])+")"); dirty_=true; }; auto *tip=element("stadium-tip"); if(type=="mousescroll") { if(!on_world)return false; auto cam=stadium_cam(outer); const double zoom=std::clamp(cam[0]*std::exp(-event.GetParameter("wheel_delta_y",0)*.16),.75,3.2); cam[1]=mx-(mx-cam[1])*zoom/cam[0];cam[2]=my-(my-cam[2])*zoom/cam[0];cam[0]=zoom; place(cam); if(tip)tip->SetProperty("display","none"); event.StopImmediatePropagation();return true; } if(type=="mousedown") { stadium_dragged_=false;hover(under(stadium_cam(outer))); if(on_world&&event.GetParameter("button",0)==0)stadium_drag_={{"x",mx},{"y",my},{"moved",false}}; return false; } if(type=="mouseup") { if(stadium_drag_.is_object()&&stadium_drag_.at("moved")==true)stadium_dragged_=true; stadium_drag_=Json(); return false; } if(stadium_drag_.is_object()) { const double dx=mx-stadium_drag_.at("x").get(),dy=my-stadium_drag_.at("y").get(); if(stadium_drag_.at("moved")!=true&&std::hypot(dx,dy)<5)return false; auto cam=stadium_cam(outer);cam[1]+=dx;cam[2]+=dy; stadium_drag_={{"x",mx},{"y",my},{"moved",true}}; place(cam);hover({}); if(tip)tip->SetProperty("display","none"); return true; } // what the pointer is on hover(under(stadium_cam(outer))); if(!tip)return false; std::string name,state; if(auto *n=part_element(stadium_hover_)){name=n->GetAttribute("data-tip-name","");state=n->GetAttribute("data-tip-state","");} const auto shown=name+"|"+state; if(name.empty()) { if(!stadium_tip_.empty()){stadium_tip_.clear();tip->SetProperty("display","none");dirty_=true;} return false; } if(stadium_tip_!=shown){stadium_tip_=shown;tip->SetInnerRML(""+esc(name)+""+esc(state)+"");} tip->SetProperty("display","block"); stadium_tip_at_={std::min(mx+16,stadium_scene::width-330),std::max(4.0,my-34)}; tip->SetProperty("left",pg(stadium_tip_at_[0]));tip->SetProperty("top",pg(stadium_tip_at_[1])); dirty_=true; return false; } bool App::stadium_action(const std::string &action,Rml::Element *target) { if(engine_mode_)return engine_stadium_action(action,target); if(!action.starts_with("stadium-"))return false; if(menu_!="stadium"||!source_||!world_||amount_||!popup_.empty()||!report_detail_kind_.empty())return true; if(stadium_focus_action(action,target))return true; stadium_view_defaults(stadium_view_);const auto s=snap();const auto club=stadium_managed(s); if(club.empty()||!s.world)return true; const auto overview=demo_stadium_view(s.state,*s.world,stadium_view_.at("future").get(),club);if(!overview.value("ok",false))return true; const auto id=target?target->GetAttribute("data-id",""):std::string(); const auto index=stadium_integer(id,25); const auto fresh=[&](){const auto now=snap();return stadium_world_&&now.world==stadium_world_&&source_.get()==stadium_source_&&now.revision==stadium_revision_&&stadium_managed(now)==stadium_view_.value("bound_club",std::string());}; const auto cancel=[&](){const auto restore=stadium_view_.at("return_focus").get();stadium_view_["dialog"]="";stadium_view_["draft"]=Json();stadium_view_["feedback"]="";stadium_view_["invalid_places"]=false;stadium_view_["return_focus"]="";stadium_world_.reset();stadium_source_=nullptr;stadium_revision_=0;focus_after_render_=restore.empty()?"stadium-data":restore;refresh();}; if(action=="stadium-cancel"){cancel();return true;} if(action=="stadium-ticket") { if(!index||stadium_view_.at("dialog")!="")return true; if(s.state.at("ticket")==*index)return true; const auto world=s.world;const auto *source=source_.get();const auto revision=s.revision;const int value=*index;const auto focus=target->GetId(); stadium_view_["ticket_return_focus"]=focus; confirmation("Eintritt auf "+std::to_string(value)+" "+money_unit()+" ändern?",[this,world,source,revision,value,focus] { if(menu_!="stadium"||source_.get()!=source||snap().world!=world) {message("Der Eintritt konnte nicht übernommen werden. Bitte den aktuellen Stadionstand erneut öffnen.");return;} const auto result=source_->apply({revision,"ticket",value}); if(!result.ok){message("Der Eintritt konnte nicht übernommen werden. Bitte den aktuellen Stadionstand erneut öffnen.");return;} stadium_view_["ticket_return_focus"]="";focus_after_render_=focus;refresh(); }); return true; } if(action=="stadium-build") { if(stadium_view_.at("dialog")==""||!stadium_view_.at("draft").is_object())return true; if(!fresh()){stadium_view_["feedback"]="Der Stadionstand hat sich geändert. Bitte den Ausbau erneut öffnen.";sync_stadium_draft();return true;} const auto q=demo_stadium_quote(s.state,stadium_view_.at("draft"),*s.world,club); if(!q.value("can_build",false)||stadium_view_.at("invalid_places").get()){sync_stadium_draft();return true;} const auto result=source_->apply({stadium_revision_,"stadium",{{"kind","build"},{"request",stadium_view_.at("draft")}}}); if(!result.ok){stadium_view_["feedback"]="Der Bauauftrag konnte nicht übernommen werden. Bitte den aktuellen Stand erneut öffnen.";sync_stadium_draft();return true;} stadium_view_["future"]=stadium_view_.at("draft").at("kind")!="periphery";stadium_view_["hint"]="";stadium_view_["return_focus"]="stadium-future";cancel();return true; } if(action=="stadium-option") { if(stadium_view_.at("dialog")!="facility"||!index||!fresh())return true; const int i=stadium_view_.at("draft").at("index").get();const auto &row=overview.at("facilities")[size_t(i)]; if(*index<=row.at("level").get()||*index>row.at("max_level").get())return true; stadium_view_["draft"]["level"]=*index;stadium_view_["feedback"]="";focus_after_render_="stadium-option-"+id;refresh();return true; } if(action=="stadium-places-editor") { if(stadium_view_.at("dialog")!="block"||!fresh())return true; const int i=stadium_view_.at("draft").at("index").get();const auto &row=overview.at("blocks")[size_t(i)]; const auto world=stadium_world_;const auto *source=stadium_source_;const auto revision=stadium_revision_; AmountRequest request;request.title="Neue Gesamtplätze";request.unit="Plätze";request.return_focus="stadium-place-number";request.value=stadium_view_.at("draft").at("places").get();request.min=row.at("places").get();request.max=row.at("max_places").get();request.step=1000;request.suggestion_step=1000;request.review=false; request.accept=[this,world,source,revision,i](int64_t value) { if(menu_!="stadium"||stadium_view_.value("dialog",std::string())!="block"||source_.get()!=source||snap().world!=world||snap().revision!=revision||stadium_revision_!=revision||!stadium_view_.at("draft").is_object()||stadium_view_.at("draft").value("index",-1)!=i||value%1000)return false; stadium_view_["draft"]["places"]=value;stadium_view_["invalid_places"]=false;stadium_view_["feedback"]="";refresh();return true; }; open_amount(std::move(request));return true; } if(stadium_view_.at("dialog")!="")return true; if(action=="stadium-periphery") { if(stadium_view_.at("view")!="panorama"||!index||*index>13||!target)return true; const auto &row=overview.at("periphery")[size_t(*index)]; if(row.at("building").get()||row.at("level").get()>=row.at("max_level").get())return true; stadium_view_["draft"]={{"kind","periphery"},{"index",*index},{"level",row.at("level").get()+1}}; stadium_view_["dialog"]="periphery";stadium_view_["bound_club"]=club;stadium_view_["feedback"]="";stadium_view_["invalid_places"]=false;stadium_view_["return_focus"]=target->GetId();stadium_world_=s.world;stadium_revision_=s.revision;stadium_source_=source_.get();focus_after_render_="stadium-build";refresh();return true; } if(action=="stadium-block"||action=="stadium-facility") { if(!index||*index>7||!target)return true; const bool block=action=="stadium-block"; const auto &row=overview.at(block?"blocks":"facilities")[size_t(*index)]; if(row.at("building").get())return true; if(!block&&row.at("level").get()>=row.at("max_level").get()){message("Diese Ausstattung ist bereits vollständig ausgebaut oder im Bau.");return true;} stadium_view_["draft"]=block?Json{{"kind","block"},{"index",*index},{"places",std::min(row.at("max_places").get(),row.at("places").get()+1000)},{"seats",row.at("seats")},{"roof",row.at("roof")}}:Json{{"kind","facility"},{"index",*index},{"level",row.at("level").get()+1}}; stadium_view_["dialog"]=block?"block":"facility";stadium_view_["bound_club"]=club;stadium_view_["feedback"]="";stadium_view_["invalid_places"]=false;stadium_view_["return_focus"]=target->GetId();stadium_world_=s.world;stadium_revision_=s.revision;stadium_source_=source_.get();focus_after_render_=block?"stadium-place-number":"stadium-option-"+std::to_string(row.at("level").get()+1);refresh();return true; } if(action=="stadium-view"){stadium_view_["view"]=stadium_view_.at("view")=="inside"?"panorama":"inside";focus_after_render_="stadium-view";} else if(action=="stadium-future"){if(stadium_view_.at("view")=="data")return true;stadium_view_["future"]=!stadium_view_.at("future").get();focus_after_render_="stadium-future";} else if(action=="stadium-data"){stadium_view_["view"]="data";focus_after_render_="stadium-back";} else if(action=="stadium-back"){stadium_view_["view"]="inside";focus_after_render_="stadium-data";} else if(action=="stadium-curve"){if(!index||*index>5)return true;stadium_view_["curves"][size_t(*index)]=!stadium_view_.at("curves")[size_t(*index)].get();focus_after_render_="stadium-curve-"+id;} else if(action=="stadium-scale"){if(!index||*index>1)return true;stadium_view_["relative"]=bool(*index);focus_after_render_="stadium-scale-"+id;} else return true; refresh();return true; } bool App::stadium_event(Rml::Event &event) { if(engine_mode_)return engine_stadium_event(event); if(menu_!="stadium"||!source_||!report_detail_kind_.empty())return false; stadium_view_defaults(stadium_view_);const auto type=event.GetType();auto *target=event.GetTargetElement(); const auto key=type=="keydown"?Rml::Input::KeyIdentifier(event.GetParameter("key_identifier",0)):Rml::Input::KI_UNKNOWN; if(!popup_.empty()) { if(stadium_view_.at("ticket_return_focus")!="") { std::string action;for(auto *n=target;n&&n!=doc_;n=n->GetParentNode())if(n->HasAttribute("data-action")){action=n->GetAttribute("data-action","");break;} if(key==Rml::Input::KI_ESCAPE||(type=="click"&&action=="popup-close")) {focus_after_render_=stadium_view_.at("ticket_return_focus").get();stadium_view_["ticket_return_focus"]="";refresh();} } return false; } if(amount_)return false; const bool dialog=stadium_view_.at("dialog")!=""; if((type=="keydown"&&key==Rml::Input::KI_ESCAPE)||(dialog&&type=="mouseup"&&event.GetParameter("button",0)==1)) { if(dialog)stadium_action("stadium-cancel",nullptr); else if(stadium_view_.at("view")=="data"||stadium_view_.at("view")=="panorama")stadium_action("stadium-back",nullptr); else return false; event.StopImmediatePropagation();return true; } if(!dialog&&stadium_stage_event(event))return true; Rml::Element *place_range=nullptr; for(auto *node=target;node&&node!=doc_;node=node->GetParentNode())if(node->GetId()=="stadium-place-range"){place_range=node;break;} if(dialog&&type=="keydown"&&place_range&&stadium_view_.at("dialog")=="block"&&(key==Rml::Input::KI_HOME||key==Rml::Input::KI_END)) { const auto s=snap(); if(s.world==stadium_world_&&source_.get()==stadium_source_&&s.revision==stadium_revision_) { const auto current=demo_stadium_view(s.state,*s.world,false,stadium_managed(s)); if(current.value("ok",false)) { const auto &row=current.at("blocks")[stadium_view_.at("draft").at("index").get()]; const int value=row.at(key==Rml::Input::KI_HOME?"places":"max_places").get(); if(value>=row.at("places").get()&&value<=row.at("max_places").get()&&value%1000==0) { stadium_view_["draft"]["places"]=value;stadium_view_["invalid_places"]=false;stadium_view_["feedback"]=""; if(auto *range=dynamic_cast(place_range))range->SetValue(std::to_string(value)); } } } sync_stadium_draft();event.StopImmediatePropagation();return true; } if(dialog&&type=="keydown"&&key==Rml::Input::KI_TAB) { Rml::ElementList controls;if(auto *modal=element("stadium-modal"))modal->QuerySelectorAll(controls,"button,input");controls.erase(std::remove_if(controls.begin(),controls.end(),[](auto *n){return n->HasAttribute("disabled")||!n->IsVisible(true);}),controls.end()); if(!controls.empty()){const auto it=std::find(controls.begin(),controls.end(),context_->GetFocusElement());const bool back=event.GetParameter("shift_key",0)!=0;const int old=it==controls.end()?(back?0:-1):int(it-controls.begin());controls[size_t((old+(back?-1:1)+int(controls.size()))%int(controls.size()))]->Focus(true);}event.StopImmediatePropagation();return true; } if(dialog&&(type=="input"||type=="change")&&target&&stadium_view_.at("dialog")=="block") { if(auto *control=dynamic_cast(target)) { if(target->GetId()=="stadium-place-range") { const auto value=stadium_integer(control->GetValue(),18000);const auto s=snap();const int i=stadium_view_.at("draft").at("index").get();const auto current=demo_stadium_view(s.state,*s.world,false,stadium_managed(s));const auto &row=current.at("blocks")[size_t(i)]; if(!value||*value%1000||*value()||*value>row.at("max_places").get())stadium_view_["invalid_places"]=true; else {stadium_view_["draft"]["places"]=*value;stadium_view_["invalid_places"]=false;stadium_view_["feedback"]="";}sync_stadium_draft();event.StopImmediatePropagation();return true; } if(target->HasAttribute("data-stadium-feature")) { const auto field=target->GetAttribute("data-stadium-feature","");if(field!="seats"&&field!="roof")return true; stadium_view_["draft"][field]=target->HasAttribute("checked");stadium_view_["feedback"]=""; sync_stadium_draft();event.StopImmediatePropagation();return true; } } } if(dialog) { bool inside=false;for(auto *n=target;n&&n!=doc_;n=n->GetParentNode())if(n->GetId()=="stadium-modal")inside=true; if(!inside&&(type=="click"||type=="dblclick"||type=="mousedown"||type=="mouseup"||type=="keydown")){event.StopImmediatePropagation();return true;} } return false; }