/*---------------------------------------------------------------------------* 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 // //////////////////////////////////////////////////// SliderBarFloat* SliderBarFloat::Add(const SliderBarFloat& item) { SliderBarFloat* pItem = new SliderBarFloat(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, SliderBarFloat::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, SliderBarFloat::Increment, pItem)); item.window->menuItems.push_back(pItem); return pItem; } void SliderBarFloat::Increment(void* data) { SliderBarFloat* bar = (SliderBarFloat*)data; bar->pos += pow(10, -bar->decimals); if (bar->pos > bar->max) bar->pos = bar->max; (*bar->target) = bar->pos; bar->func(bar->funcExtra); } void SliderBarFloat::Decrement(void* data) { SliderBarFloat* bar = (SliderBarFloat*)data; bar->pos -= pow(10, -bar->decimals); if (bar->pos < bar->min) bar->pos = bar->min; (*bar->target) = bar->pos; bar->func(bar->funcExtra); } // Updates the slider bar bool SliderBarFloat::Update() { float totalDistance = this->max - this->min; // In case something caused us to change float diff = this->pos - *this->target; if (diff > totalDistance / 1000 || diff < -totalDistance / 1000) 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; 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 SliderBarFloat::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]; switch (this->decimals) { case 0: sprintf(buffer, "%.0f", this->pos); break; case 1: sprintf(buffer, "%.1f", this->pos); break; case 2: sprintf(buffer, "%.2f", this->pos); break; case 3: sprintf(buffer, "%.3f", this->pos); break; case 4: sprintf(buffer, "%.4f", this->pos); break; case 5: sprintf(buffer, "%.5f", this->pos); break; case 6: sprintf(buffer, "%.6f", this->pos); break; case 7: sprintf(buffer, "%.7f", this->pos); break; case 8: sprintf(buffer, "%.8f", this->pos); break; case 9: sprintf(buffer, "%.9f", this->pos); break; case 10: sprintf(buffer, "%.10f", this->pos); break; default: OSHalt("Invalid number of decimal places specified!"); break; } 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 SliderBarFloat::Reset() { if (this->status > 0) this->status = 0; float totalDistance = this->max - this->min; // In case something caused us to change float diff = this->pos - *this->target; if (diff > totalDistance / 1000 || diff < -totalDistance / 1000) this->pos = *this->target; }