Beispiel #1
0
void set_char_at(char c, int line_num, int  x)
{
	uint8_t orig_cursor=cursor_pos;
	//move cursor
	cursorTo(line_num, x);
	//write
	dataWrite(c);
	//restore cursor
	commandWrite(CMD_DDADDR(orig_cursor));
	cursor_pos=orig_cursor;
}
void Dialog::Marketplace(bool fromTradingPost)
{
    Display & display = Display::Get();
    const ICN::icn_t tradpost = Settings::Get().EvilInterface() ? ICN::TRADPOSE : ICN::TRADPOST;
    const std::string & header = _("Marketplace");

    Cursor & cursor = Cursor::Get();
    cursor.Hide();
    cursor.SetThemes(cursor.POINTER);

    Dialog::Box box(260, true);

    const Rect & pos_rt = box.GetArea();
    Point dst_pt(pos_rt.x, pos_rt.y);
    Rect dst_rt(pos_rt);
    Text text;

    // header
    text.Set(header, Font::BIG);
    dst_pt.x = pos_rt.x + (pos_rt.w - text.w()) / 2;
    dst_pt.y = pos_rt.y;
    text.Blit(dst_pt);

    TradeWindowGUI gui(pos_rt);

    Kingdom & kingdom = world.GetMyKingdom();
    const Sprite & spritecursor = AGG::GetICN(tradpost, 14);

    const std::string & header_from = _("Your Resources");

    Resource::funds_t fundsFrom = kingdom.GetFundsResource();
    u8 resourceFrom = 0;
    const Point pt1(pos_rt.x, pos_rt.y + 190);
    std::vector<Rect> rectsFrom(7);
    rectsFrom[0] = Rect(pt1.x, pt1.y, 34, 34);		// wood
    rectsFrom[1] = Rect(pt1.x + 37, pt1.y, 34, 34);	// mercury
    rectsFrom[2] = Rect(pt1.x + 74, pt1.y, 34, 34);	// ore
    rectsFrom[3] = Rect(pt1.x, pt1.y + 37, 34, 34);	// sulfur
    rectsFrom[4] = Rect(pt1.x + 37, pt1.y + 37, 34, 34);// crystal
    rectsFrom[5] = Rect(pt1.x + 74, pt1.y + 37, 34, 34);// gems
    rectsFrom[6] = Rect(pt1.x + 37, pt1.y + 74, 34, 34);// gold
    SpriteCursor cursorFrom(spritecursor);
    text.Set(header_from, Font::SMALL);
    dst_pt.x = pt1.x + (108 - text.w()) / 2;
    dst_pt.y = pt1.y - 15;
    text.Blit(dst_pt);
    RedrawFromResource(pt1, fundsFrom);

    const std::string & header_to = _("Available Trades");

    Resource::funds_t fundsTo;
    u8 resourceTo = 0;
    const Point pt2(138 + pos_rt.x, pos_rt.y + 190);
    std::vector<Rect> rectsTo(7);
    rectsTo[0] = Rect(pt2.x, pt2.y, 34, 34);		// wood
    rectsTo[1] = Rect(pt2.x + 37, pt2.y, 34, 34);	// mercury
    rectsTo[2] = Rect(pt2.x + 74, pt2.y, 34, 34);	// ore
    rectsTo[3] = Rect(pt2.x, pt2.y + 37, 34, 34);	// sulfur
    rectsTo[4] = Rect(pt2.x + 37, pt2.y + 37, 34, 34);	// crystal
    rectsTo[5] = Rect(pt2.x + 74, pt2.y + 37, 34, 34);	// gems
    rectsTo[6] = Rect(pt2.x + 37, pt2.y + 74, 34, 34);	// gold
    SpriteCursor cursorTo(spritecursor);
    text.Set(header_to, Font::SMALL);
    dst_pt.x = pt2.x + (108 - text.w()) / 2;
    dst_pt.y = pt2.y - 15;
    text.Blit(dst_pt);
    RedrawToResource(pt2, false, fromTradingPost);

    u32 count_sell = 0;
    u32 count_buy = 0;
    
    u32 max_sell = 0;
    u32 max_buy = 0;

    Button & buttonTrade = gui.buttonTrade;
    Button & buttonLeft = gui.buttonLeft;
    Button & buttonRight = gui.buttonRight;
    Splitter & splitter = gui.splitter;

    // button exit
    const Sprite & sprite_exit = AGG::GetICN(tradpost, 17);
    dst_pt.x = pos_rt.x + (pos_rt.w - sprite_exit.w()) / 2;
    dst_pt.y = pos_rt.y + pos_rt.h - sprite_exit.h();
    Button buttonExit(dst_pt, tradpost, 17, 18);

    buttonExit.Draw();

    cursor.Show();
    display.Flip();

    LocalEvent & le = LocalEvent::Get();
   
    // message loop
    while(le.HandleEvents())
    {
        if(buttonTrade.isEnable()) le.MousePressLeft(buttonTrade) ? buttonTrade.PressDraw() : buttonTrade.ReleaseDraw();
        if(buttonLeft.isEnable()) le.MousePressLeft(buttonLeft) ? buttonLeft.PressDraw() : buttonLeft.ReleaseDraw();
        if(buttonRight.isEnable()) le.MousePressLeft(buttonRight) ? buttonRight.PressDraw() : buttonRight.ReleaseDraw();

        le.MousePressLeft(buttonExit) ? buttonExit.PressDraw() : buttonExit.ReleaseDraw();

        if(le.MouseClickLeft(buttonExit) || le.KeyPress(KEY_RETURN) || le.KeyPress(KEY_ESCAPE)) break;
	
        // click from
        for(u8 ii = 0; ii < rectsFrom.size(); ++ii)
        {
            const Rect & rect_from = rectsFrom[ii];
            
            if(le.MouseClickLeft(rect_from))
            {
                switch(ii)
                {
                    case 0:
                        resourceFrom = Resource::WOOD;
                        max_sell = fundsFrom.wood;
                        break;
                    case 1:
                        resourceFrom = Resource::MERCURY;
                        max_sell = fundsFrom.mercury;
                        break;
                    case 2:
                        resourceFrom = Resource::ORE;
                        max_sell = fundsFrom.ore;
                        break;
                    case 3:
                        resourceFrom = Resource::SULFUR;
                        max_sell = fundsFrom.sulfur;
                        break;
                    case 4:
                        resourceFrom = Resource::CRYSTAL;
                        max_sell = fundsFrom.crystal;
                        break;
                    case 5:
                        resourceFrom = Resource::GEMS;
                        max_sell = fundsFrom.gems;
                        break;
                    case 6:
                        resourceFrom = Resource::GOLD;
                        max_sell = fundsFrom.gold;
                        break;
                    default: break;
                }
                
                if(GetTradeCosts(resourceFrom, resourceTo, fromTradingPost))
                {
                    max_buy = Resource::GOLD == resourceTo ? 
                        max_sell * GetTradeCosts(resourceFrom, resourceTo, fromTradingPost) :
                        max_sell / GetTradeCosts(resourceFrom, resourceTo, fromTradingPost);
                }
                
                count_sell = 0;
                count_buy = 0;
                
                cursor.Hide();
                cursorFrom.Move(rect_from.x - 2, rect_from.y - 2);
                cursorFrom.Show();
                
                if(resourceTo) cursorTo.Hide();
                RedrawToResource(pt2, true, fromTradingPost, resourceFrom);
                if(resourceTo) cursorTo.Show();
                if(resourceTo) gui.ShowTradeArea(resourceFrom, resourceTo, max_buy, max_sell, count_buy, count_sell, fromTradingPost);
                
                cursor.Show();
                display.Flip();
            }
        }
        
        // click to
        for(u8 ii = 0; ii < rectsTo.size(); ++ii)
        {
            const Rect & rect_to = rectsTo[ii];
            
            if(le.MouseClickLeft(rect_to))
            {
                switch(ii)
                {
                    case 0: resourceTo = Resource::WOOD; break;
                    case 1: resourceTo = Resource::MERCURY; break;
                    case 2: resourceTo = Resource::ORE; break;
                    case 3: resourceTo = Resource::SULFUR; break;
                    case 4: resourceTo = Resource::CRYSTAL; break;
                    case 5: resourceTo = Resource::GEMS; break;
                    case 6: resourceTo = Resource::GOLD; break;
                    default: break;
                }
                
                if(GetTradeCosts(resourceFrom, resourceTo, fromTradingPost))
                {
                    max_buy = Resource::GOLD == resourceTo ? 
                        max_sell * GetTradeCosts(resourceFrom, resourceTo, fromTradingPost) :
                        max_sell / GetTradeCosts(resourceFrom, resourceTo, fromTradingPost);
                }
                
                count_sell = 0;
                count_buy = 0;
                
                cursor.Hide();
                cursorTo.Move(rect_to.x - 2, rect_to.y - 2);
                
                if(resourceFrom)
                {
                    cursorTo.Hide();
                    RedrawToResource(pt2, true, fromTradingPost, resourceFrom);
                    cursorTo.Show();
                    gui.ShowTradeArea(resourceFrom, resourceTo, max_buy, max_sell, count_buy, count_sell, fromTradingPost);
                }
                cursor.Show();
                display.Flip();
            }
        }
        
        // move splitter
        if(buttonLeft.isEnable() && buttonRight.isEnable() && max_buy && le.MousePressLeft(splitter.GetRect()))
        {
            u32 seek = (le.GetMouseCursor().x - splitter.GetRect().x) * 100 / splitter.GetStep();
            
            if(seek < splitter.Min()) seek = splitter.Min();
            else
            if(seek > splitter.Max()) seek = splitter.Max();
            
            count_buy = seek * (Resource::GOLD == resourceTo ? GetTradeCosts(resourceFrom, resourceTo, fromTradingPost) : 1);
            count_sell = seek * (Resource::GOLD == resourceTo ? 1: GetTradeCosts(resourceFrom, resourceTo, fromTradingPost));
            
            cursor.Hide();
            splitter.Move(seek);
            gui.RedrawInfoBuySell(count_sell, count_buy);
            cursor.Show();
            display.Flip();
        }
        
        // trade
        if(buttonTrade.isEnable() && le.MouseClickLeft(buttonTrade) && count_sell && count_buy)
        {
            kingdom.OddFundsResource(Resource::funds_t(static_cast<Resource::resource_t>(resourceFrom), count_sell));
            kingdom.AddFundsResource(Resource::funds_t(static_cast<Resource::resource_t>(resourceTo), count_buy));
#ifdef WITH_NET
	    FH2LocalClient::SendMarketSellResource(kingdom, resourceFrom, count_sell, fromTradingPost);
	    FH2LocalClient::SendMarketBuyResource(kingdom, resourceTo, count_buy, fromTradingPost);
#endif            
            resourceTo = resourceFrom = Resource::UNKNOWN;
            gui.ShowTradeArea(resourceFrom, resourceTo, 0, 0, 0, 0, fromTradingPost);

            fundsFrom = kingdom.GetFundsResource();
            cursorTo.Hide();
            cursorFrom.Hide();
            RedrawFromResource(pt1, fundsFrom);
            RedrawToResource(pt2, false, fromTradingPost, resourceFrom);
            display.Flip();
        }
        
        // decrease trade resource
        if(count_buy &&
           ((buttonLeft.isEnable() && le.MouseClickLeft(gui.buttonLeft)) ||
            le.MouseWheelDn(splitter.GetRect())))
        {
            count_buy -= Resource::GOLD == resourceTo ? GetTradeCosts(resourceFrom, resourceTo, fromTradingPost) : 1;
            
            count_sell -= Resource::GOLD == resourceTo ? 1: GetTradeCosts(resourceFrom, resourceTo, fromTradingPost);
            
            cursor.Hide();
            splitter.Backward();
            gui.RedrawInfoBuySell(count_sell, count_buy);
            cursor.Show();
            display.Flip();
        }
        
        // increase trade resource
        if( count_buy < max_buy &&
            ((buttonRight.isEnable() && le.MouseClickLeft(buttonRight)) ||
             le.MouseWheelUp(splitter.GetRect())))
        {
            count_buy += Resource::GOLD == resourceTo ? GetTradeCosts(resourceFrom, resourceTo, fromTradingPost) : 1;
            
            count_sell += Resource::GOLD == resourceTo ? 1: GetTradeCosts(resourceFrom, resourceTo, fromTradingPost);
            
            cursor.Hide();
            splitter.Forward();
            gui.RedrawInfoBuySell(count_sell, count_buy);
            cursor.Show();
            display.Flip();            
        }
    }
}