From 0e9e6fb18bcec1723858c830ef5d045d9e27f3cf Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Wed, 16 Jul 2014 17:46:43 +0000 Subject: [PATCH] DroidFish: Implemented function to reset all UCI options to default values. --- DroidFish/res/layout-land/editoptions.xml | 7 + DroidFish/res/layout/editoptions.xml | 9 +- DroidFish/res/values/strings.xml | 1 + .../droidfish/activities/EditOptions.java | 258 ++++++++++-------- 4 files changed, 167 insertions(+), 108 deletions(-) diff --git a/DroidFish/res/layout-land/editoptions.xml b/DroidFish/res/layout-land/editoptions.xml index 5649eb9..7098f08 100644 --- a/DroidFish/res/layout-land/editoptions.xml +++ b/DroidFish/res/layout-land/editoptions.xml @@ -34,5 +34,12 @@ android:layout_height="wrap_content" android:layout_weight="1"> + diff --git a/DroidFish/res/layout/editoptions.xml b/DroidFish/res/layout/editoptions.xml index a081618..646aff0 100644 --- a/DroidFish/res/layout/editoptions.xml +++ b/DroidFish/res/layout/editoptions.xml @@ -33,5 +33,12 @@ android:layout_height="wrap_content" android:layout_weight="1"> - + + diff --git a/DroidFish/res/values/strings.xml b/DroidFish/res/values/strings.xml index 39c9343..04a5cb9 100644 --- a/DroidFish/res/values/strings.xml +++ b/DroidFish/res/values/strings.xml @@ -130,6 +130,7 @@ you are not actively using the program.\ Toggle Large Buttons Toggle Blindfold Mode Cancel + Reset Yes No Move number: diff --git a/DroidFish/src/org/petero/droidfish/activities/EditOptions.java b/DroidFish/src/org/petero/droidfish/activities/EditOptions.java index 3924d51..1df21ab 100644 --- a/DroidFish/src/org/petero/droidfish/activities/EditOptions.java +++ b/DroidFish/src/org/petero/droidfish/activities/EditOptions.java @@ -93,115 +93,118 @@ public class EditOptions extends Activity { LinearLayout content = (LinearLayout)findViewById(R.id.eo_content); Button okButton = (Button)findViewById(R.id.eo_ok); Button cancelButton = (Button)findViewById(R.id.eo_cancel); + Button resetButton = (Button)findViewById(R.id.eo_reset); - for (String name : uciOpts.getOptionNames()) { - UCIOptions.OptionBase o = uciOpts.getOption(name); - if (!o.visible) - continue; - switch (o.type) { - case CHECK: { - View v = View.inflate(this, R.layout.uci_option_check, null); - CheckBox checkBox = (CheckBox)v.findViewById(R.id.eo_value); - checkBox.setText(o.name); - final UCIOptions.CheckOption co = (UCIOptions.CheckOption)o; - checkBox.setChecked(co.value); - checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - co.set(isChecked); - } - }); - content.addView(v); - break; - } - case SPIN: { - View v = View.inflate(this, R.layout.uci_option_spin, null); - TextView label = (TextView)v.findViewById(R.id.eo_label); - EditText value = (EditText)v.findViewById(R.id.eo_value); - final UCIOptions.SpinOption so = (UCIOptions.SpinOption)o; - String labelText = String.format(Locale.US, "%s (%d\u2013%d)", so.name, so.minValue, so.maxValue); - label.setText(labelText); - value.setText(so.getStringValue()); - if (so.minValue >= 0) - value.setInputType(android.text.InputType.TYPE_CLASS_NUMBER); - value.addTextChangedListener(new TextWatcher() { - public void onTextChanged(CharSequence s, int start, int before, int count) { } - public void beforeTextChanged(CharSequence s, int start, int count, int after) { } - @Override - public void afterTextChanged(Editable s) { - try { - int newVal = Integer.parseInt(s.toString()); - if (newVal < so.minValue) - so.set(so.minValue); - else if (newVal > so.maxValue) - so.set(so.maxValue); - else - so.set(newVal); - } catch (NumberFormatException ex) { + if (uciOpts != null) { + for (String name : uciOpts.getOptionNames()) { + UCIOptions.OptionBase o = uciOpts.getOption(name); + if (!o.visible) + continue; + switch (o.type) { + case CHECK: { + View v = View.inflate(this, R.layout.uci_option_check, null); + CheckBox checkBox = (CheckBox)v.findViewById(R.id.eo_value); + checkBox.setText(o.name); + final UCIOptions.CheckOption co = (UCIOptions.CheckOption)o; + checkBox.setChecked(co.value); + checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + co.set(isChecked); } - } - }); - content.addView(v); - break; - } - case COMBO: { - View v = View.inflate(this, R.layout.uci_option_combo, null); - TextView label = (TextView)v.findViewById(R.id.eo_label); - Spinner value = (Spinner)v.findViewById(R.id.eo_value); - label.setText(o.name); - final UCIOptions.ComboOption co = (UCIOptions.ComboOption)o; - ArrayAdapter adapter = - new ArrayAdapter(this, android.R.layout.simple_spinner_item, - co.allowedValues); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - value.setAdapter(adapter); - value.setSelection(adapter.getPosition(co.value)); - value.setOnItemSelectedListener(new OnItemSelectedListener() { - @Override - public void onItemSelected(AdapterView av, View view, int position, long id) { - if ((position >= 0) && (position < co.allowedValues.length)) - co.set(co.allowedValues[position]); - } - public void onNothingSelected(AdapterView arg0) { } - }); - content.addView(v); - break; - } - case BUTTON: { - View v = View.inflate(this, R.layout.uci_option_button, null); - ToggleButton button = (ToggleButton)v.findViewById(R.id.eo_label); - final UCIOptions.ButtonOption bo = (UCIOptions.ButtonOption)o; - bo.trigger = false; - button.setText(o.name); - button.setTextOn(o.name); - button.setTextOff(o.name); - button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - bo.trigger = isChecked; - } - }); - content.addView(v); - break; - } - case STRING: { - View v = View.inflate(this, R.layout.uci_option_string, null); - TextView label = (TextView)v.findViewById(R.id.eo_label); - EditText value = (EditText)v.findViewById(R.id.eo_value); - label.setText(o.name + " "); - final UCIOptions.StringOption so = (UCIOptions.StringOption)o; - value.setText(so.value); - value.addTextChangedListener(new TextWatcher() { - public void onTextChanged(CharSequence s, int start, int before, int count) { } - public void beforeTextChanged(CharSequence s, int start, int count, int after) { } - @Override - public void afterTextChanged(Editable s) { - so.set(s.toString()); - } - }); - content.addView(v); - break; - } + }); + content.addView(v); + break; + } + case SPIN: { + View v = View.inflate(this, R.layout.uci_option_spin, null); + TextView label = (TextView)v.findViewById(R.id.eo_label); + EditText value = (EditText)v.findViewById(R.id.eo_value); + final UCIOptions.SpinOption so = (UCIOptions.SpinOption)o; + String labelText = String.format(Locale.US, "%s (%d\u2013%d)", so.name, so.minValue, so.maxValue); + label.setText(labelText); + value.setText(so.getStringValue()); + if (so.minValue >= 0) + value.setInputType(android.text.InputType.TYPE_CLASS_NUMBER); + value.addTextChangedListener(new TextWatcher() { + public void onTextChanged(CharSequence s, int start, int before, int count) { } + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } + @Override + public void afterTextChanged(Editable s) { + try { + int newVal = Integer.parseInt(s.toString()); + if (newVal < so.minValue) + so.set(so.minValue); + else if (newVal > so.maxValue) + so.set(so.maxValue); + else + so.set(newVal); + } catch (NumberFormatException ex) { + } + } + }); + content.addView(v); + break; + } + case COMBO: { + View v = View.inflate(this, R.layout.uci_option_combo, null); + TextView label = (TextView)v.findViewById(R.id.eo_label); + Spinner value = (Spinner)v.findViewById(R.id.eo_value); + label.setText(o.name); + final UCIOptions.ComboOption co = (UCIOptions.ComboOption)o; + ArrayAdapter adapter = + new ArrayAdapter(this, android.R.layout.simple_spinner_item, + co.allowedValues); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + value.setAdapter(adapter); + value.setSelection(adapter.getPosition(co.value)); + value.setOnItemSelectedListener(new OnItemSelectedListener() { + @Override + public void onItemSelected(AdapterView av, View view, int position, long id) { + if ((position >= 0) && (position < co.allowedValues.length)) + co.set(co.allowedValues[position]); + } + public void onNothingSelected(AdapterView arg0) { } + }); + content.addView(v); + break; + } + case BUTTON: { + View v = View.inflate(this, R.layout.uci_option_button, null); + ToggleButton button = (ToggleButton)v.findViewById(R.id.eo_label); + final UCIOptions.ButtonOption bo = (UCIOptions.ButtonOption)o; + bo.trigger = false; + button.setText(o.name); + button.setTextOn(o.name); + button.setTextOff(o.name); + button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + bo.trigger = isChecked; + } + }); + content.addView(v); + break; + } + case STRING: { + View v = View.inflate(this, R.layout.uci_option_string, null); + TextView label = (TextView)v.findViewById(R.id.eo_label); + EditText value = (EditText)v.findViewById(R.id.eo_value); + label.setText(o.name + " "); + final UCIOptions.StringOption so = (UCIOptions.StringOption)o; + value.setText(so.value); + value.addTextChangedListener(new TextWatcher() { + public void onTextChanged(CharSequence s, int start, int before, int count) { } + public void beforeTextChanged(CharSequence s, int start, int count, int after) { } + @Override + public void afterTextChanged(Editable s) { + so.set(s.toString()); + } + }); + content.addView(v); + break; + } + } } } @@ -218,6 +221,47 @@ public class EditOptions extends Activity { finish(); } }); + resetButton.setOnClickListener(new OnClickListener() { + @Override + public void onClick(View v) { + if (uciOpts != null) { + boolean modified = false; + for (String name : uciOpts.getOptionNames()) { + UCIOptions.OptionBase o = uciOpts.getOption(name); + if (!o.visible) + continue; + switch (o.type) { + case CHECK: { + UCIOptions.CheckOption co = (UCIOptions.CheckOption)o; + if (co.set(co.defaultValue)) + modified = true; + break; + } + case SPIN: { + UCIOptions.SpinOption so = (UCIOptions.SpinOption)o; + if (so.set(so.defaultValue)) + modified = true; + break; + } + case COMBO: { + UCIOptions.ComboOption co = (UCIOptions.ComboOption)o; + if (co.set(co.defaultValue)) + modified = true; + break; + } + case STRING: { + UCIOptions.StringOption so = (UCIOptions.StringOption)o; + if (so.set(so.defaultValue)) + modified = true; + break; + } + } + } + if (modified) + initUI(); + } + } + }); } private final void sendBackResult() {