很长一段时间以来,我一直在为这个问题烦恼,基本上我已经创建了一个利润分配系统,其中包括允许许多合作伙伴将他们的股份设置在 0-100 的滑块上的功能。
我遇到问题的地方是阻止滑块设置不切实际的值,即在一个合作伙伴拥有 100% 的股份集的情况下。目前,我正在 Partner 类滑块事件侦听器中使用以下代码进行测试:
// check we don't use more responsibility shares than available
if(rs_sldr.value < availableShares + rs_sldr.value) {
// update shares value if valid
RSShares = rs_sldr.value;
}
availableShares 值取自一般合作伙伴类,每次调整合作伙伴滑块时都会更新,方法是将所有合作伙伴份额值相加并从总可用份额中扣除 (100)。
我在 if 语句中添加滑块值以创建正确的比例,就好像我只使用可用的共享值一样,滑块可以为第一个合作伙伴设置不超过 50,因为在 50 时只剩下 50 个共享,if 语句被触发。我敢肯定有一个更合乎逻辑的方法来做到这一点!!!
无论如何,这是我的代码,希望有人能把我从这给我造成的痛苦中解救出来。非常感谢。
普通合伙人类:
package com.example.profitdistributiondiagram {
import flash.display.MovieClip;
import flash.events.Event;
import com.example.profitdistributiondiagram.events.PartnerEvent;
import flash.globalization.CurrencyFormatter;
import flash.globalization.LocaleID;
public class Partners extends MovieClip {
public var TPGrossPerc:Number = 0;
public var TPGrossAmount:Number = 0;
public var RSShareValue:Number = 0;
public var RSGrossShareAmount:Number = 0;
public var RSGrossAmount:Number = 0;
public var RSNetAmount:Number = 0;
public var PPGrossPoints:Number = 0;
public var PPGrossAmount:Number = 0;
public var DGrossPerc:Number = 0;
public var DGrossAmount:Number = 0;
public var partners = new Array();
public var FDAmount:Number = 0;
public var totalInputRS:Number = 0;
public var totalInputPP:Number = 0;
public var totalInputD:Number = 0;
public var availableShares:Number = 100;
private var cf:CurrencyFormatter;
public function Partners() {
cf = new CurrencyFormatter( "en-UK" );
cf.setCurrency("EUR", "€");
cf.fractionalDigits = 0;
cf.negativeSymbol = '-';
cf.negativeCurrencyFormat = 1;
// set names
partner_1.row.partner_ref.text = "P1";
partner_2.row.partner_ref.text = "P2";
partner_3.row.partner_ref.text = "P3";
partner_4.row.partner_ref.text = "P4";
partner_5.row.partner_ref.text = "P5";
// set partner parent
partner_1.row.addEventListener(PartnerEvent.UPDATE,partnerRowUpdateListener);
partner_2.row.addEventListener(PartnerEvent.UPDATE,partnerRowUpdateListener);
partner_3.row.addEventListener(PartnerEvent.UPDATE,partnerRowUpdateListener);
partner_4.row.addEventListener(PartnerEvent.UPDATE,partnerRowUpdateListener);
partner_5.row.addEventListener(PartnerEvent.UPDATE,partnerRowUpdateListener);
// init partners
partners = [partner_1,partner_2,partner_3,partner_4,partner_5];
}
private function partnerRowUpdateListener(e:Event) {
updateSubtotals();
updateMaxValues();
updateLabels();
updateBarCharts();
}
public function update() {
updateSubtotals();
updateMaxValues();
updateLabels();
updateBarCharts();
}
private function updateMaxValues() {
var p,partner;
// loop through partners and add values together
for(var i:Number = 0; i < partners.length; i++) {
partner = partners[i].row;
partner.availableShares = availableShares;
partner.updateNoEvent();
}
}
private function updateSubtotals() {
// reset vars
TPGrossPerc = 0;
TPGrossAmount = 0;
RSGrossShareAmount = 0;
RSGrossAmount = 0;
RSNetAmount = 0;
PPGrossPoints = 0;
PPGrossAmount = 0;
DGrossPerc = 0;
DGrossAmount = 0;
var p,partner;
// loop through partners and add values together
for(var i:Number = 0; i < partners.length ; i++) {
partner = partners[i];
p = partners[i].row;
p.FDAmount = FDAmount;
p.totalInputRS = totalInputRS;
p.totalInputPP = totalInputPP;
p.totalInputD = totalInputD;
p.updateNoEvent();
if(partner.configEnabled) {
// add up totals
TPGrossPerc += p.TPPerc;
TPGrossAmount += p.TPGrossAmount;
trace("partner: "+i+" "+p.RSShares);
RSGrossShareAmount += p.RSShares;
RSGrossAmount += p.RSGrossAmount;
RSNetAmount += p.RSGrossMinusTPDrawings;
PPGrossPoints += p.PPoints;
PPGrossAmount += p.PPAmount;
DGrossPerc += p.DPerc;
DGrossAmount += p.DAmount;
}
//TPGrossPerc = Math.max(100,TPGrossPerc);
//TPGrossAmount = Math.max(totalInput
//RSNetAmount = Math.min(totalInputRS
//RSGrossAmount = Math.min(totalInputRS,RSGrossAmount);
}
trace('RSGrossShareAmount: '+RSGrossShareAmount);
// set available shares
availableShares = 100 - RSGrossShareAmount;
}
private function updateLabels() {
// set values
tp_gross_perc_lbl.text = String(TPGrossPerc);
tp_gross_amount_lbl.text = cf.format(TPGrossAmount,true);
rs_share_value_lbl.text = cf.format(totalInputRS / 100,true);
rs_gross_shares_lbl.text = String(RSGrossShareAmount);
rs_gross_amount_lbl.text = cf.format(RSGrossAmount,true);
rs_net_amount_lbl.text = cf.format(RSNetAmount,true);
pp_gross_points_lbl.text = String(PPGrossPoints);
pp_gross_amount_lbl.text = cf.format(PPGrossAmount,true);
d_gross_total_lbl.text = String(DGrossPerc);
d_gross_amount_lbl.text = cf.format(DGrossAmount,true);
}
private function updateBarCharts() {
var bcHeight = 190;
var bcOnePerc = bcHeight / 100;
var p;
var greatestSalary = 0;
var maxScale = 0;
// loop through partners and add values together
for(var i:Number = 0; i < partners.length ; i++) {
p = partners[i];
if(p.row.salary > greatestSalary) {
greatestSalary = p.row.salary;
}
}
// set max scale
maxScale = greatestSalary;
// loop through partners and add values together
for(i = 0; i < partners.length ; i++) {
p = partners[i];
// update label
if(p.bar_chart_mc != null) {
p.bar_chart_mc.amount_txt.text = cf.format(p.row.salary,true);
// get percentages
//trace('');
//trace('p.row.FDAmount: '+p.row.FDAmount);
//trace('p.row.salary: '+p.row.salary);
//trace('p.row.FDAmount / p.row.salary: '+(p.row.FDAmount / maxScale) * 100);
var fdPerc = !((p.row.FDAmount / maxScale) * 100) ? 0 : (p.row.FDAmount / maxScale) * 100;
var rsPerc = !((p.row.RSGrossMinusTPDrawings / maxScale) * 100) ? 0 : (p.row.RSGrossMinusTPDrawings / maxScale) * 100;
var tpPerc = !((p.row.TPGrossAmount / maxScale ) * 100) ? 0 : (p.row.TPGrossAmount / maxScale) * 100;
var ppPerc = !((p.row.PPAmount / maxScale) * 100) ? 0 : (p.row.PPAmount / maxScale) * 100;
var dPerc = !((p.row.DAmount / maxScale) * 100) ? 0 : (p.row.DAmount / maxScale) * 100;
//trace('per: '+(fdPerc + rsPerc + tpPerc + ppPerc + dPerc));
p.bar_chart_mc.fixed_drawings_mc.height = Math.round(bcOnePerc * fdPerc);
p.bar_chart_mc.topup_drawings_mc.height = Math.round(bcOnePerc * tpPerc);
p.bar_chart_mc.responsibility_shares_mc.height = Math.round(bcOnePerc * rsPerc);
p.bar_chart_mc.performance_points_mc.height = Math.round(bcOnePerc * ppPerc);
p.bar_chart_mc.discretionary_mc.height = Math.round(bcOnePerc * dPerc);
p.bar_chart_mc.topup_drawings_mc.y = p.bar_chart_mc.fixed_drawings_mc.y - p.bar_chart_mc.fixed_drawings_mc.height;
p.bar_chart_mc.responsibility_shares_mc.y = p.bar_chart_mc.topup_drawings_mc.y - p.bar_chart_mc.topup_drawings_mc.height;
p.bar_chart_mc.performance_points_mc.y = p.bar_chart_mc.responsibility_shares_mc.y - p.bar_chart_mc.responsibility_shares_mc.height;
p.bar_chart_mc.discretionary_mc.y = p.bar_chart_mc.performance_points_mc.y - p.bar_chart_mc.performance_points_mc.height;
//trace('p.bar_chart_mc.fixed_drawings_mc.y: '+p.bar_chart_mc.fixed_drawings_mc.y);
//trace('pp.bar_chart_mc.topup_drawings_mc.y: '+p.bar_chart_mc.topup_drawings_mc.y);
}
}
}
}
}
合作伙伴类:
package com.example.profitdistributiondiagram {
import flash.display.MovieClip;
import fl.events.SliderEvent;
import com.example.profitdistributiondiagram.events.PartnerEvent;
import flash.events.Event;
import flash.events.TextEvent;
import flash.globalization.CurrencyFormatter;
import flash.globalization.LocaleID;
public class PartnerRow extends MovieClip {
public var availableShares:Number = 100;
public var FDAmount:Number = 0;
public var TPGrossAmount:Number = 0;
public var TPPerc:Number = 0;
public var RSGrossAmount:Number = 0;
public var RSShares:Number = 0;
public var RSGrossMinusTPDrawings:Number = 0;
public var PPGrossAmount:Number = 0;
public var PPoints:Number = 0;
public var PPAmount:Number = 0;
public var PPGrossPoints:Number = 100;
public var DGrossAmount:Number = 0;
public var DPerc:Number = 0;
public var DAmount:Number = 0;
public var totalInputRS:Number = 0;
public var totalInputPP:Number = 0;
public var totalInputD:Number = 0;
public var salary:Number = 0;
public var enableControl;
public var barChart:MovieClip;
private var cf:CurrencyFormatter;
public function PartnerRow() {
// set currency formatter
cf = new CurrencyFormatter( "en-UK" );
cf.setCurrency("EUR", "€");
cf.fractionalDigits = 0;
cf.negativeSymbol = '-';
cf.negativeCurrencyFormat = 1;
// event listeners
tp_sldr.addEventListener(SliderEvent.THUMB_DRAG, sliderDragHandler);
rs_sldr.addEventListener(SliderEvent.THUMB_DRAG, sliderDragHandler);
pp_sldr.addEventListener(SliderEvent.THUMB_DRAG, sliderDragHandler);
d_sldr.addEventListener(SliderEvent.THUMB_DRAG, sliderDragHandler);
// update
update();
}
private function sliderDragHandler(event:SliderEvent):void {
trace('availableShares: '+availableShares);
trace(' availableShares: '+(availableShares + rs_sldr.value));
trace('rs_sldr.value: '+rs_sldr.value);
// check we don't use more shares than available
if(rs_sldr.value < availableShares + rs_sldr.value) {
// update shares value if valid
RSShares = rs_sldr.value;
//trace("RSShares: "+RSShares);
}
trace('max: '+rs_sldr.maximum);
// update labels
update();
}
public function update() {
// update but don't dispatch an event
updateNoEvent();
// dispatch update event
this.dispatchEvent(new Event(PartnerEvent.UPDATE));
}
public function updateNoEvent() {
// set responsibility shares
//RSShares = rs_sldr.value;
RSGrossAmount = (totalInputRS / 100) * RSShares;
RSGrossMinusTPDrawings = RSGrossAmount - TPGrossAmount;
// set RS text values
rs_shares_lbl.text = String(RSShares);
/*rs_gross_amount_lbl.text = cf.format(RSGrossAmount,true);
rs_net_amount_lbl.text = cf.format(RSGrossMinusTPDrawings,true);
// set topup drawings
TPGrossAmount = (RSGrossAmount / 100) * tp_sldr.value;
TPPerc = tp_sldr.value;
tp_perc_lbl.text = String(TPPerc);
tp_gross_amount_lbl.text = cf.format(TPGrossAmount,true);
// set performance points
PPGrossAmount = totalInputPP;
PPoints = pp_sldr.value;
PPAmount = (PPGrossAmount / PPGrossPoints) * PPoints;
// set PP text values
pp_points_input.text = String(PPoints);
pp_amount_lbl.text = cf.format(PPAmount,true);
// set discretionary
DGrossAmount = totalInputD;
DPerc = d_sldr.value;
DAmount = (DGrossAmount / 100) * DPerc;
// ser discretionary text values
d_perc_input.text = String(DPerc);
d_amount_input.text = cf.format(DAmount,true);
*/
// set salary
salary = FDAmount + RSGrossMinusTPDrawings + TPGrossAmount + PPAmount + DAmount;
}
}
}