/*---------------------------------------------------------------------------* Copyright (C) 2010-2012 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ //////////////////////////////////////////////////// // // Slider Bar functions // //////////////////////////////////////////////////// SliderBar* SliderBar::Add(const SliderBar& item) { SliderBar* pItem = new SliderBar(item); pItem->minus = HeldButton::Add(HeldButton(item.window, CVec3(item.x - (item.width - item.height) / 2, item.y, item.z + 0.01), CVec2(item.height, item.height), WhiteDarker, SliderBar::Decrement, pItem)); pItem->plus = HeldButton::Add(HeldButton(item.window, CVec3(item.x + (item.width - item.height) / 2, item.y, item.z + 0.01), CVec2(item.height, item.height), WhiteDarker, SliderBar::Increment, pItem)); item.window->menuItems.push_back(pItem); return pItem; } void SliderBar::Increment(void* data) { SliderBar* bar = (SliderBar*)data; bar->pos ++; bar->pos = (int)(bar->pos + 10000000 + 0.5) - 10000000; if (bar->pos > bar->max) bar->pos = bar->max; (*bar->target) = (int)(bar->pos + 10000000 + 0.5) - 10000000; bar->func(bar->funcExtra); } void SliderBar::Decrement(void* data) { SliderBar* bar = (SliderBar*)data; bar->pos --; bar->pos = (int)(bar->pos + 10000000 + 0.5) - 10000000; if (bar->pos < bar->min) bar->pos = bar->min; (*bar->target) = (int)(bar->pos + 10000000 + 0.5) - 10000000; bar->func(bar->funcExtra); } // Updates the slider bar bool SliderBar::Update() { // In case something caused us to change float diff = this->pos - *this->target; if (diff > 0.5 || diff < -0.5) this->pos = *this->target; ///////////////////////////////////////////////////////////////////////////////////////////// // The slider itself if (!PositivePressed()) this->status = 0; if (this->status == 2) { float x = CursorX() - this->oldX; float t = x / (this->width - this->height * 3); this->pos = (t + 0.5) * (max - min) + min; // Cap it! if (this->pos < min) this->pos = min; if (this->pos > max) this->pos = max; (*this->target) = (this->pos + 0.5); this->func(this->funcExtra); return true; } int status; // We don't care as much if the cursor isn't being triggered if (PositiveTriggered()) status = 2; else status = 1; // Normalize it between -0.5 and 0.5 float t = (float)(this->pos - min) / (max - min) - 0.5; // And scale it to find the center of the slider float x = t * (this->width - this->height * 3); // See if we just hit something if (CursorColliding(this->x + x, this->y, this->height, this->height)) this->status = status; else this->status = 0; if (this->status == 2) this->oldX = CursorX() - x; return (this->status == 0); } // Draws the slider bar void SliderBar::Draw() { // The connection DrawQuad(position, CVec2(this->width - this->height / 3, this->height / 4), CVec4(0, 0, 0)); // Handle the buttons if (this->pos <= min) { this->pos = min; this->minus->status = -1; } else if (this->minus->status < 0) this->minus->status = 0; if (this->pos >= max) { this->pos = max; this->plus->status = -1; } else if (this->plus->status < 0) this->plus->status = 0; // Just to be nice, update the slider's button positions this->minus->x = this->x - (this->width - this->height) / 2; this->minus->y = this->y; this->plus->x = this->x + (this->width - this->height) / 2; this->plus->y = this->y; ///////////////////////////////////////////////////////////////////////////////////////////// // Normalize it between -0.5 and 0.5 float t = (float)(this->pos - min) / (max - min) - 0.5; // And scale it to find the center of the slider float x = t * (this->width - this->height * 3); float color = 0.8 - 0.2 * this->status; DrawBox(CVec3(this->x + x, this->y, this->z + 0.002), CVec2(this->height, this->height), CVec4(color, color, color)); char buffer[30]; sprintf(buffer, "%i", (int)(this->pos + 0.5)); DrawTextCenter(buffer, CVec3(this->x + x, this->y, this->z + 0.003), CVec2(this->height, this->height * 0.7), CVec4(0, 0, 0)); // Make sure we have the right arrow textures! this->minus->tex = GetTexture(CW_TEXTURE_SLIDERLEFT); this->plus->tex = GetTexture(CW_TEXTURE_SLIDERRIGHT); } // Resets the slider void SliderBar::Reset() { if (this->status > 0) this->status = 0; // In case something caused us to change float diff = this->pos - *this->target; if (diff > 0.5 || diff < -0.5) this->pos = *this->target; }