void WelcomeScene::ccTouchesEnded(CCSet* touches,CCEvent* event){ CCTouch* touch = (CCTouch*)touches->anyObject(); CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); if(touchId != -1){ for(int i = 0;i < itemsArray->count();i++){ ShopItem* item = (ShopItem*)itemsArray->objectAtIndex(i); if(item->isTouch(location.x - scroll->getPositionX() - scroll->getContentOffset().x, location.y - scroll->getPositionY())){ if(temp != NULL){ temp->setSelected(false); } if(item->getId() == touchId){ if(item->touchAction() == 0){ createItems(item->getType(),scroll->getContentOffset().x); temp = NULL; }else{ temp = item; char t[100]; if(item->getValue() < 10){ sprintf(t,"购买物品需要%d元人民币,点击右侧的买入按钮即可购买",item->getValue()); }else{ sprintf(t,"购买物品需要%d仙桃,点击右侧的买入按钮即可购买",item->getValue()); } tipText->setString(conv(t)); temp->setSelected(true); } } break; } } } touchId = -1; }
ShopItem *ShopItems::findItem(int id) { ShopItem *item; std::vector<ShopItem*>::iterator it; for(it = mShopItems.begin(); it != mShopItems.end(); it++) { item = *(it); if (item->getId() == id) { return item; } } return 0; }
void WelcomeScene::ccTouchesBegan(CCSet* touches,CCEvent* event){ CCTouch* touch = (CCTouch*)touches->anyObject(); CCPoint location = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView()); for(int i = 0;i < itemsArray->count();i++){ ShopItem* item = (ShopItem*)itemsArray->objectAtIndex(i); if(item->isTouch(location.x - scroll->getPositionX() - scroll->getContentOffset().x, location.y - scroll->getPositionY())){ /*if(item->touchAction() == 0){ createItems(item->getType()); }*/ touchId = item->getId(); lastPt = ccp(location.x,location.y); break; } } }
void ShopItems::addItem(int inventoryIndex, int id, int quantity, int price) { ShopItem *item = 0; if (mMergeDuplicates) { item = findItem(id); } if (item) { item->addDuplicate (inventoryIndex, quantity); } else { item = new ShopItem(inventoryIndex, id, quantity, price); mShopItems.push_back(item); } }
void ShopItemFactory::updateItem(ListView* list, bb::cascades::VisualNode *listItem, const QString &type, const QVariantList &indexPath, const QVariant &data) { Q_UNUSED(list); Q_UNUSED(indexPath); Q_UNUSED(type); // Update the control with the correct data. QVariantMap map = data.value<QVariantMap>(); ShopItem *recipeItem = static_cast<ShopItem *>(listItem); //recipeItem->updateItem(map["fruit"].toString()); ImageLoader *imloader = new ImageLoader(); imloader->loadImage(map["productthumb"].toString(), recipeItem->itemImage(),"/products/",".png"); //set labels recipeItem->setTitle(map["productname"].toString()); recipeItem->setDescription("Credits: " + map["productprice"].toString() + "\nCards: "+map["productnumcards"].toString()); }
void WelcomeScene::createItems(int type,float offset){ temp = NULL; CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("shop.plist","shop.png"); if(scroll){ removeChild(scroll,true); } itemsLayer = CCLayer::create(); int x = 0; int count = 0; int max = 0; bool next = true; int value = 0; if(!itemsArray){ itemsArray = CCArray::create(); itemsArray->retain(); }else{ itemsArray->removeAllObjects(); } while(next){ count++; //CCSprite* bg = CCSprite::createWithSpriteFrameName("item_bg.png"); ShopItem* item = NULL; char name[20]; switch(type){ case WEAPON: max = 7; sprintf(name,"weapon/weapon%d.png",count - 1); if(count < 5){ value = 1000 * count; }else{ value = 2; } break; case TREASURE: max = 1; return; break; case TIP: max = 3; sprintf(name,"tip/tip%d.png",count * 3); value = 1500 * count; break; case CLOTHES: max = 4; sprintf(name,"hero_%d.png",(count - 1)); if(count == 4){ value = 2; }else{ value = 800 * count; } break; default: CCLog("type %d error",type); break; } item = new ShopItem(x,0,name,value,type,count - 1); itemsLayer->addChild(item->getLayer()); itemsArray->addObject(item); x += item->getWidth() + 10; if(count >= max){ next = false; } } itemsLayer->setContentSize(CCSizeMake(x,480 - 150)); SETANCHPOS(itemsLayer,0,0,0,0); scroll = CCScrollView::create(); SETANCHPOS(scroll,175,150,0,0); scroll->setDirection(kCCScrollViewDirectionHorizontal); scroll->setViewSize(CCSizeMake(max > 3 ? 854 - 150 : x,480 - 150)); scroll->setContentSize(CCSizeMake(x,480 - 150)); scroll->setContainer(itemsLayer); scroll->setContentOffset(ccp(offset,0)); addChild(scroll); }
void SellDialog::action(const gcn::ActionEvent &event) { if (event.getId() == "quit") { close(); return; } int selectedItem = mShopItemList->getSelected(); // The following actions require a valid item selection if (selectedItem == -1 || selectedItem >= (int) mShopItems->getNumberOfElements()) { return; } if (event.getId() == "slider") { mAmountItems = (int) mSlider->getValue(); updateButtonsAndLabels(); } else if (event.getId() == "+" && mAmountItems < mMaxItems) { mAmountItems++; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "-" && mAmountItems > 1) { mAmountItems--; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "max") { mAmountItems = mMaxItems; mSlider->setValue(mAmountItems); updateButtonsAndLabels(); } else if (event.getId() == "sell" && mAmountItems > 0 && mAmountItems <= mMaxItems) { // Attempt sell ShopItem *item = mShopItems->at(selectedItem); int sellCount, itemIndex; mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice(); mMaxItems -= mAmountItems; while (mAmountItems > 0) { // This order is important, item->getCurrentInvIndex() would return // the inventory index of the next Duplicate otherwise. itemIndex = item->getCurrentInvIndex(); sellCount = item->sellCurrentDuplicate(mAmountItems); Net::getNpcHandler()->sellItem(current_npc, itemIndex, sellCount); mAmountItems -= sellCount; } mPlayerMoney += mAmountItems * mShopItems->at(selectedItem)->getPrice(); mAmountItems = 1; mSlider->setValue(0); if (!mMaxItems) { // All were sold mShopItemList->setSelected(-1); delete mShopItems->at(selectedItem); mShopItems->erase(selectedItem); gcn::Rectangle scroll; scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1); scroll.height = mShopItemList->getRowHeight(); mShopItemList->showPart(scroll); } else { mSlider->gcn::Slider::setScale(1, mMaxItems); // Update only when there are items left, the entry doesn't exist // otherwise and can't be updated updateButtonsAndLabels(); } } }